Recently in learning Java, talk about the understanding of abstract classes in learning, if there are mistakes and also please big guy correct.
There are subclasses and superclasses in Java inheritance. Superclasses and subclasses can be said to be general and special, abstract and concrete. For example, fruits and bananas, people and students, cars and red flags, mobile phones and Huawei.
Taking Dog and Bird classes as examples, each animal has its own name or species, so we can introduce a common superclass, and we can put the repeated methods in Dog and Bird classes at a higher level in the inheritance hierarchy. Alternatively, we can add a getDescription method to the common superclass that gives a brief description of the animal. Now we’re going to set a super class Animal.
But how do you set the getDescription method if you only know the name of the Animal in Animal and know nothing about other descriptions? You can use the abstract keyword at this point, so you don’t need to implement this method at all, leaving the implementation code to subclasses.
public abstract class Animal { /*..... */ public abstract void getDescription(); // Abstract method}Copy the code
To improve program clarity, classes that contain one or more abstract methods must themselves be declared abstract. As in Animal, the getDescription() method is abstract, so the class must also be declared abstract.
In addition to abstract methods, abstract classes can also contain concrete fields and concrete methods.
public abstract class Animal { private String name; public Animal(String _name) { this.name=_name; } public String getName() { return this.name; } public abstract void getDescription(); // Abstract method}Copy the code
Dog and Bird classes are defined by extending the abstract Animal class and implementing the method getDescription class. Since there are no abstract methods in Dog and Bird classes, there is no need to declare them as abstract classes.
A class can be declared as abstract even without abstract methods.
An abstract class cannot be instantiated, but an object that can be created as a concrete subclass.
Animal dog1 = new Animal(" dog2 "); Animal dog1 = new Dog(" little black "," woof woof "," Alaska "); // Dog1 is an abstract Animal variable that references an instance of a non-abstract subclass DogCopy the code
Below the interview test code.
public abstract class Animal { private String name; public Animal(String _name) { this.name=_name; } public String getName() { return this.name; } public abstract void getDescription(); // Abstract method}Copy the code
public class Bird extends Animal { private String color; private int age; public Bird(String _name,String _color,int _age) { super(_name); color = _color; age = _age; } @override public void getDescription() {system.out.println (" age "+ "color "+" color "+ "fly "); }}Copy the code
public class Dog extends Animal { private String call; private String kind; public Dog(String _name,String _call,String _kind) { super(_name); call=_call; kind=_kind; } @override public void getDescription() {system.out.println (getName() +" +kind+" + call); }}Copy the code
public class Test { public static void main(String[] args) { var animal = new Animal[2]; Animal [0] = new Dog(animal[0] = new Dog(animal[0]); Animal [1] = new Bird("小红"," 小红", 5); For (Animal a: Animal)// Call getDescription() directly from a; }}Copy the code