Java abstract classes and interfaces

First, before we understand abstract classes and interfaces, we need to know what inheritance is? What is rewriting? ...

First, before we understand abstract classes and interfaces, we need to know what inheritance is? What is rewriting?

After knowing the abstract classes and interfaces, we need to know what polymorphism is?

(mainly understand how to implement polymorphism with abstract classes and interfaces)

catalogue

1. Succession

2. Rewrite

3. Abstract class

4. Interface

5. Polymorphism

Get to the point:

1. Succession

We all know that Java is an object-oriented programming language, and the object is the class in Java.

What is inheritance?

for instance:

I first create a Dog class, which has name and leg attribute fields, as well as a cry method and an eat method.

In creating a Bird class, there are also name and wing attribute fields, as well as a fly method and an eat method.

//This is the class we normally create public class Dog { public String name; //name public String leg; //How many legs public void cry(){ System.out.println(this.name+"Barking!"); } public void eat(){ System.out.println(this.name+"Eat something"); } } public class Bird { public String name; //name public String wing; //wing public void fly(){ System.out.println(this.name+"Can fly"); } public void eat(){ System.out.println(this.name+"Eat something"); } }

By observing the above two classes, we can find that they have common properties and methods. Imagine that if we need a lot of classes and happen to have a lot of duplicate code in these classes, we will waste a lot of time.

So the inheritance came out.

Handle the above code with inheritance:

//The Dog subclass inherits from the Animal parent class public class Dog extends Animal{ public String leg; //How many legs public Dog(String name){ super(name); } public void cry(){ System.out.println(this.name+"Barking!"); } } //The Bird subclass inherits from the Animal parent class public class Bird extends Animal{ public String wing; //wing public Bird(String name) { super(name); } public void fly(){ System.out.println(this.name+"Can fly"); } } //Parent class public class Animal { public String name; public Animal(String name) { this.name = name; } public void eat(){ System.out.println(name+"Eat something"); } }

In the above code, we put the repetitive attributes and methods in dog class and Bird class into a separate Animal class, and then let Dog class and Bird class inherit the Animal class using the extends keyword.

At this time, we call Dog class and Bird class subclasses (or derived classes)

An Animal class is called a parent class (or a base class or a superclass)

So how to use the properties and methods of the parent class?

//Create a test class public class Test { public static void main(String[] args) { Dog dog = new Dog("Xiaobai"); System.out.println(dog.name); dog.eat(); Bird bird = new Bird("Xiao Huang"); System.out.println(bird.name); bird.fly(); } }

Operation results:

  It indicates that the subclass can call the methods and properties of the parent class.

2. Rewrite

Through the above code, we can find that the subclass can call the methods and properties of the inherited parent class.

But what if the method of the parent class also exists in the child class?

When this method is called, is the method of the child class or the method of the parent class running?

Here's a try:

//We write their eat methods in both Dog and Bird classes public class Dog extends Animal{ public String leg; //How many legs public Dog(String name){ super(name); } public void cry(){ System.out.println(this.name+"Barking!"); } public void eat(){ System.out.println("I'm a subclass Dog of eat method!"); } } public class Bird extends Animal{ public String wing; //wing public Bird(String name) { super(name); } public void fly(){ System.out.println(this.name+"Can fly"); } public void eat(){ System.out.println("I'm a subclass Bird of eat method!"); } } //The eat method of the parent class remains unchanged public class Animal { public String name; public Animal(String name) { this.name = name; } public void eat(){ System.out.println(name+"Eat something"); } }

Then call the test class:

public class Test { public static void main(String[] args) { Dog dog = new Dog("Xiaobai"); System.out.println(dog.name); dog.eat(); Bird bird = new Bird("Xiao Huang"); System.out.println(bird.name); bird.eat(); } }

Operation results:

Note as long as the subclass and parent class have the same method (return type, method name and parameter list), the subclass's own method will be called when the subclass calls the method.

We call this phenomenon: override, that is, the subclass overrides the method of the parent class.

3. Abstract class

In the above example, the parent class of Animal has the common methods and properties of the child classes Dog and Bird. And the only function of the parent class Animal is to be inherited by the child class.

Therefore, in Java, a name is given to this class, and an abstract keyword is used to modify it -- abstract class

Abstract classes cannot be instantiated and can only be inherited by other entity classes.

In addition to ordinary methods and properties, abstract classes also have abstract methods, which are also decorated with abstract.

So what is an abstract method?

Better understand with examples:

//This is an abstract class modified by the abstract keyword public abstract class Animal { public String name; //Common member variable public Animal(String name) { //Construction method this.name = name; } public void run(){ //Common method System.out.println("Run"); } //This is an abstract method modified by the abstract keyword. It has no body and must be overridden by subclasses. public abstract void eat(); }

At the same time, it should be noted that classes with abstract methods must be abstract classes, and abstract classes do not necessarily have abstract methods.

4. Interface

We know what an abstract class is, so the interface is easy to understand.

Interface is a special abstract class. Why is it special?

Because if a class is an interface, it must be an abstract class. On the contrary, if a class is an abstract class, it is not necessarily an interface.

(an interface needs to be decorated with the keyword interface)

The following is an example of the class Animal:

//This is an interface modified by the interface keyword public interface Animal { //public static final modified attribute constant. public static final String name = "Xiao Huang"; int age = 20; //In the interface, whether you write or not, the property is modified by public static final by default //There can only be abstract methods in the interface. After the entity class implements this interface, it must override this abstract method public abstract void eat(); //Or the method modified by default, and the entity class does not need to override this method default void run(){ } //Or static methods public static void cry(){ System.out.println("Cry"); } }

  When we know how to define an interface, we wonder what it can do?

We know that in Java, an entity class can only inherit one parent class. So sometimes we will find that there are many parent classes of a thing. Therefore, in order to solve this problem, a concept - interface is introduced into Java.

Here is the relationship between classes:

There is an inheritance relationship between entity classes and abstract classes.

The relationship between entity classes and interfaces is implementation.

Interfaces and classes are also inheritance relationships.

In this way, an entity class can inherit an abstract class and implement multiple interfaces. So as to meet its required methods and properties

5. Polymorphism

Conceptually speaking, it is "a reference can show many different forms"

Through examples, we can better understand:

//Defines the parent class of a drawing abstract class Shape{ public abstract void print(); //This is an abstract method that needs to be rewritten } //triangle class Triangle extends Shape{ //Override the print method of the parent class @Override public void print() { System.out.println("△"); } } //rectangle class Rectangle extends Shape{ //Override the print method of the parent class @Override public void print() { System.out.println("□"); } } //circle class Circle extends Shape{ //Override the print method of the parent class @Override public void print() { System.out.println("○"); } } //Using polymorphism requires two conditions //1. Upward Transformation: parent class references child class objects // For example, Shape x = new Triangle(); //2. Override the parent class method. public class Test { public static void draw(Shape x){ //Here is the reference of the parent object x.print(); } public static void main(String[] args) { Triangle triangle = new Triangle(); Rectangle rectangle = new Rectangle(); Circle circle = new Circle(); //Polymorphism occurs when the draw method is called draw(triangle); //What is passed is a reference to a subclass object draw(rectangle); draw(circle); } }

Sometimes to understand a concept, you don't need to use very complex examples. On the contrary, the simpler it is, the easier it is to understand.

2 December 2021, 18:52 | Views: 2694

Add new comment

For adding a comment, please log in
or create account

0 comments