Day 14 of learning java

Keywords: Java

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 classes and methods must be decorated with the abstract keyword

          public abstract class name {}

         public abstract void eat();

② Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes

③ Abstract classes cannot be instantiated directly, but they can be instantiated through subclass objectification by referring to polymorphism, which is called abstract polymorphism

④ Subclasses of abstract classes, or override all abstract methods in abstract classes, if they are abstract classes  

Characteristics of members in abstract classes: member variables can be variables or constants

                                    There is a constructor, but it cannot be instantiated. It is used for the initialization of subclass accessing parent class data

Case: cat and dog (Abstract version)

Requirements: use the idea of abstract class to realize the case of cat and dog, and test it in the test class  

Other codes are similar to polymorphic versions. Only different animal classes (parent classes) are pasted here. The codes are as follows

public abstract class Animal {
    public String name;
    public 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();
}

Interface: decorated with keyword interface         public interface interface name {}

The class implementation interface is represented by implements         public class class name implements interface name {}

Interface cannot be instantiated: instantiate class objects by referring to polymorphism, which is called interface polymorphism

                            Forms of polymorphism: concrete class polymorphism, abstract class polymorphism, interface polymorphism

                            The premise of polymorphism: there is inheritance or realization relationship; There are ways to rewrite; There are parent (class / interface) applications pointing to (child / implementation) class objects

Implementation class of interface: either rewrite all abstract methods in the interface, or it is an abstract class

Characteristics of interface members:

① Member variable: can only be constant. Default modifier: public static final

② Construction method: there is no construction method for the interface, because the interface mainly abstracts behavior, and there is no specific class. If there is no parent class, it inherits from Object by default

③ Member method: can only be abstract method, default modifier: public abstract

Case: cat and dog (interface version)

Requirements: after training cats and dogs, they can jump. The high jump function is added here. Please use abstract classes and interfaces to implement cat and dog cases and test them in the test class

Idea: define interfaces

public interface Jumpping {
    public abstract jump();
}

Defining Abstract animal classes

public class Aniaml {
    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 eat();
}

  Define specific cat class, inherit animal class and realize high jump interface

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("Cat high jump");
    }
}

Define the dog class, inherit the animal class, and implement the high jump interface

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("Dog high jump");
    }
}

Define test classes and write code to test

public class AnimalDemo{
    public static void main(String[] args) {
        //Create an object and call a method
        Jumpping j = new Cat();
        j.jump();
        System.out.println("------------");

        Animal a = new Cat();
        a.setName("Garfield");
        a.setAge(5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        //a.jump(); Error: unable to access

        a = new Cat("Garfield",5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("------------");
    
        //Most frequently used, with the most methods
        Cat c = new Cat();
        c.setName("Garfield");
        c.setAge(5);
        System.out.println(c.getName()+","+c.getAge());
        c.eat();
        c.jump();
    }
}

Relationship between class and interface:

Class and class relationship: inheritance relationship, which can only be inherited, but can be inherited at multiple levels.

Relationship between class and interface: the implementation relationship can be implemented alone or multiple, and multiple interfaces can be implemented while inheriting a class.

Interface and interface relationship: inheritance relationship, single inheritance or multiple inheritance.

Differences between abstract classes and interfaces:

Member difference

        abstract class                         Variable, constant; There are construction methods; There are abstract and non abstract methods

        Interface                              Constant; Abstract method

Relationship difference

        Class and class                           Single inheritance

        Classes and interfaces                       Implementation, can be single or multiple

        Interface and interface                     Inheritance, single inheritance, multiple inheritance

Difference of design concept

        abstract class                             Abstract classes, including properties and behaviors

        Interface                                 Abstract behavior, mainly behavior

Posted by sofasurfer on Sun, 26 Sep 2021 16:49:35 -0700