The second day of Java foundation -- polymorphism, interface

Keywords: Java

polymorphic

Overview of polymorphism

Different forms of the same object at different times

The premise and embodiment of polymorphism

  • With inheritance / implementation relationship
  • There is a method rewrite relationship
  • There are parent class references to child class objects
    public class Animal {

        public void eat() {
            System.out.println("Animals eat");
        }
    }
    public class Cat extends Animal {

        @override
         public void eat() {
            System.out.println("Cats eat fish.");
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            Animal a = new Cat();
        }
    }

Characteristics of member access in polymorphism

  • Member variables: compile to the left, execute to the left
  • Member method: compile to the left, execute to the right

Why is member variable and member method access different

  • Because member methods are rewritten, and member variables are different?
    public class Animal {

        public int age = 40;
        public void eat() {
            System.out.println("Animals eat");
        }
    }
    public class Cat extends Animal {

        public int age = 20;
        public int weight = 10;

        @override
         public void eat() {
            System.out.println("Cats eat fish.");

        public void playGame() {
            System.out.println("The cat catches mice.");
        }
    }
    public class Dog extends Animal {

        @override
         public void eat() {
            System.out.println("Dogs eat bones.");

        public void lookDoor() {
            System.out.println("Dog guard");
        }
    }
    public class AnimalOperator {
        public void useAnimal(Animal a){
            //Animal a = new Cat();
            //Animal a = new Dog();
            a.eat;
        }
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            Animal a = new Cat();

            System.out.println(a.age));
 //           System.out.println(a.weight));  

            a.eat();
//            a.playGame();

            AnimalOperator ao = new AnimalOperator();
            Dog d = new Dog();
            ao.useAnimal(d);
        }
    }

Advantages and disadvantages of polymorphism

  • Benefits of polymorphism: improve the extensibility of the program (when defining methods, use the parent type as a parameter, and in the future, use specific subtypes to participate in the operation)
  • The drawback of polymorphism: the specific function of subclass cannot be used

Transformation in polymorphism

Upward Transformation:

  • From child to parent, the parent reference points to the child object

Transition down:

  • From parent to child, parent class reference to child class object
    public class Animal {

        public void eat() {
            System.out.println("Animals eat");
        }
    }
    public class Cat extends Animal {

        @override
         public void eat() {
            System.out.println("Cats eat fish.");

        public void playGame() {
            System.out.println("The cat catches mice.");
        }
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            //polymorphic
            Animal a = new Cat(); //Upward transformation
            a.eat();

            /*
            //Creating objects of Cat class
            Cat c = new Cat();
            c.eat();
            c.playGame();
            */

            //Downward transformation
            Cat c = (Cat)a;
            c.eat();
            c.playGame();
        }
    }

abstract class

Abstract class overview

In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be defined as an abstract class.

Abstract class features

  • Abstract class and abstract class method must be decorated with abstract keyword

    public abstract class name []

        public abstract void eat();
  • There is no abstract method in an abstract class. A class with an abstract method must be an abstract class
  • Abstract class cannot be instantiated

    How do abstract classes implement instantiation? Refer to the way of polymorphism and instantiate by subclass object, which is called abstract class polymorphism
  • Subclass of abstract class

    Either override all abstract methods in an abstract class
    Either an abstract class

Member characteristics of abstract classes

  • Member variables

    Can be a variable

    Can also be a constant
  • Construction method

    There is a constructor, but it cannot be instantiated

    The constructor is used to access the initialization of the parent class data
  • Member method

    There can be abstract methods: to restrict subclasses to complete certain actions

    There are also non abstract ways to improve code reusability

Cat and dog Abstract case

    //Create animal class
    public abstract class Animal {
        private String name;
        private int age;

        public Animal() {

        }

        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    
        public abstract void eat();
    }
    //Creating cats
    public class Cat extends Animal {
        
        public Cat() {
        }

        public Cat(String name, int age) {
            super(name, age);
        }

        @Override
        public void eat() {
            System.out.println("Cats eat fish.");      
        }
    }
    public class Dog extends Animal{
        
        public Dog() {
        }

        public Dog(String name, int age) {
            super(name, age);
        }

        @Override
        public void eat() {
            System.out.println("Dogs eat bones."); 
        }   
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            //Create objects in a polymorphic way
            Animal a = new Cat();
            a.setName("Muppet");
            a.setAge(3);
            System.out.println(a.getName()+","+a.getAge());
            a.eat();
            System.out.println("========");
        
            a = new Cat("Muppet",3);
            System.out.println(a.getName()+","+a.getAge());
            a.eat();
        }
    }

Interface

Interface Overview

Interface is a kind of public standard. As long as it conforms to the standard, everyone can use it

The interface in Java is more embodied in the abstraction of behavior

Features of the interface

  • Interface decorated with keyword interfa

    public interface interface name {}
  • Class implementation interface is represented by implements

    public class class name implements interface name {}
  • Interface cannot be instantiated

    How to instantiate an interface Referring to the way of polymorphism, through the implementation of class object instantiation, this is called interface polymorphism.

    Form of polymorphism: concrete class polymorphism, abstract class polymorphism, interface polymorphism
  • Implementation class of interface

    Either override all abstract methods in the interface

    Either an abstract class

Member characteristics of the interface

  • Member variables

    Can only be constant

    Default modifier: public static final
  • Construction method

    There is no construction method for the interface, because the interface mainly abstracts the behavior, and there is no concrete existence

    If a class does not explicitly inherit a parent class, it inherits the Object class by default
  • Member method

    Only abstract methods

    Default modifier: public abstract

Cat and dog interface cases

    //Defining interface Jumpping
    public interface Jumpping{
        public abstract void jump();
    }
    //Create animal class
    public abstract class Animal {
        private String name;
        private int age;

        public Animal() {

        }

        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    
        public abstract void eat();
    }
    //Creating cats
    public class Cat extends Animal implements Jumpping {
        
        public Cat() {
        }

        public Cat(String name, int age) {
            super(name, age);
        }

        @Override
        public void eat() {
            System.out.println("Cats eat fish.");      
        }

        @Override
        public void jump() {
            System.out.println("The cat can jump");       
        }
    }
    public class Dog extends Animal implements Jumpping {
        
        public Dog() {
        }

        public Dog(String name, int age) {
            super(name, age);
        }

        @Override
        public void eat() {
            System.out.println("Dogs eat bones."); 
        }

        @Override
        public void jump() {
            System.out.println("The dog can jump");       
        }   
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            //Create object, call method
            Jumpping j = new Cat();
            j.jump();
            System.out.println("========");

            Animal a = new Cat();
            a.setName("Muppet");
            a.setAge(3);
            System.out.println(a.getName()+","+a.getAge());
            a.eat();
                
            a = new Cat("Muppet",3);
            System.out.println(a.getName()+","+a.getAge());
            a.eat();
            System.out.println("========");

            Cat c = new Cat();
            c.setName("Muppet");
            c.setAge(3);
            System.out.println(a.getName()+","+a.getAge());
            c.eat();
            c.jump();
        }
    }

Relationship between class and interface

  • Class and class relationship

    Inheritance relationship. You can only inherit one, but you can inherit multiple levels
  • Class and class interface relationship

    The implementation relationship can be implemented in a single way or in multiple ways. It can also inherit a class and implement multiple interfaces at the same time
  • Interface and interface relationship

    Inheritance relationship. You can inherit one or more

The difference between abstract class and interface

  • Difference between members

    Abstract class: variable, constant; construction method; abstract method and non abstract method

    Interface: constant; abstract method
  • Relationship difference

    Classes and classes: inheritance, single inheritance

    Class and interface: implementation, can be single or multiple

    Interface and interface: inherit, single inherit, multi inherit
  • Design concept difference

    Abstract class: abstract class, including properties and behaviors

    Interface: abstract behavior, mainly behavior

Posted by Cleibe on Wed, 13 Nov 2019 10:25:59 -0800