A detailed example of the Java inheritance method

  • 2020-06-23 00:30:41
  • OfStack

继承 Is a cornerstone of java's object-oriented programming technology because it allows the creation of hierarchical classes. Inheritance can be understood as the process of one object getting properties from another.

If class A is the parent of class B and class B is the parent of class C, we also call C a subclass of A, and class C is inherited from class A. In Java, class inheritance is single-1, that is, a subclass can only have one parent

The two most commonly used keywords in inheritance are extends and implements.

The use of these two keywords determines whether an object and another object are IS-ES21en (one).

By using these two keywords, we can realize that one object gets the properties of another object.

All Java classes are inherited from the ES27en.lang.Object class, so Object is the ancestor class of all classes, and all classes except Object must have one parent class.

With the extends keyword, you can declare that a class is derived from another class, in the following general form:


// A.java
public class A {
  private int i;
  protected int j; 
  public void func() {
   }
} 
// B.java
public class B extends A {
}

The above code snippet shows that B is inherited from A, and B is a subclass of A. A is a subclass of Object and can be declared here without showing.

As a subclass, an instance of B has all of A's member variables, but no access to B's member variable, which guarantees the encapsulation of A.

IS - A relationship

IS-A means that an object is a class of another object.

Below is the implementation of inheritance using the keyword extends.


public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}

Based on the above example, the following statement is true:

The Animal class is the parent of the Mammal class.

The Animal class is the parent of the Reptile class.

The Mammal and Reptile classes are subclasses of the Animal class.

The Dog class is both a subclass of Mammal class and a subclass of Animal class.

The ES89en-ES90en relationship in the above example is analyzed as follows:

Mammal IS-A Animal

Reptile IS-A Animal

Dog IS-A Mammal

Therefore: Dog ES106en-ES107en Animal

By using the keyword extends, subclasses can inherit all methods and properties of the parent class, but not the methods and properties of private(private).
Using the instanceof operator, we were able to determine Mammal ES116en-ES117en Animal

The instance


public class Dog extends Mammal{
  public static void main(String args[]){
   Animal a = new Animal();
   Mammal m = new Mammal();
   Dog d = new Dog();
   System.out.println(m instanceof Animal);
   System.out.println(d instanceof Mammal);
   System.out.println(d instanceof Animal);
  }
}

The compilation and operation results of the above instances are as follows:


true
true
true

Having covered the extends keyword, let's look at how the implements keyword is used to represent the ES130en-ES131en relationship.

The Implements keyword is used in cases where the class inherits the interface, the extends keyword cannot be used in this case.

The instance


public interface Animal {}
public class Mammal implements Animal{
}
public class Dog extends Mammal{
}

instanceof keyword

You can use the instanceof operator to verify that the Mammal and dog objects are an instance of the Animal class.


interface Animal{}
class Mammal implements Animal{}
public class Dog extends Mammal{
  public static void main(String args[]){
   Mammal m = new Mammal();
   Dog d = new Dog();
   System.out.println(m instanceof Animal);
   System.out.println(d instanceof Mammal);
   System.out.println(d instanceof Animal);
  }
} 

The compilation and operation results of the above instances are as follows:


true
true
true

HAS - A relationship

HAS-A represents the membership between the class and its members. This helps code reuse and reduces code errors.

example


public class Vehicle{}
public class Speed{}
public class Van extends Vehicle{
 private Speed sp;
} 

The Van and Speed classes are ES171en-ES172en relationships (Van has one Speed), so you don't have to paste the entire Speed class code into the Van class, and the Speed class can be reused for multiple applications.

In the object-oriented feature, the user does not have to worry about how the class is implemented internally.

The Van class hides the details of the implementation from the user, so the user only needs to know how to call the Van class to perform certain functions, not whether the Van class does the work itself or calls another class to do the work.

Java only supports single inheritance, that is, one class cannot inherit from more than one class.

The following practices are illegal:

public class extends Animal, Mammal{}

Java only supports single inheritance (basic and abstract classes), but we can implement it with interfaces (multi-inheritance interfaces). The script structure is as follows:

public class Apple extends Fruit implements Fruit1, Fruit2{}

Generally, we use extends for basic and abstract classes, and implements for interface classes.

This article friends can refer to the following


Related articles: