Java inheritance


Inheritance is a cornerstone of Java object-oriented programming technology because it allows the creation of hierarchical classes. Inheritance can be understood as the process by which one object obtains attributes from another object.

If class A is the parent class of class B, and class B is the parent class of class C, we also say that C is a subclass of A, and class C inherits from class A. In Java, class inheritance is single inheritance, that is to say, a subclass can only have one parent class

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

The use of these two keywords determines whether an object has an IS-A (is one) relationship with another object.

By using these two keywords, we can achieve one object to obtain the properties of another object.

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

You can declare that a class inherits another class through the extends keyword. The general form is as follows:

// 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 inherits from A, and B Is a subclass of A. And A is a subclass of Object, which can be declared without explicit declaration here.


As a subclass, the instance of B has all the member variables of A, but has no access rights to the private member variable B, which ensures the encapsulation of A.



IS-A relationship

IS-A means: one object is a classification of another object.

The following 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 correct:

  • ##             Animal class is the parent class of Mammal class.

  •             Animal class is the parent class of Reptile class.

  •             Mammal class and Reptile class are subclasses of Animal class.

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

Analyze the IS-A relationship in the above example, as follows:

    ## Mammal IS-A Animal
  •             Reptile IS-A Animal
  •           Dog IS-A Mammal
  • Thus: Dog IS-A Animal

By using the keyword

extends

, a subclass can inherit all the properties of the parent class Methods and properties, but private methods and properties cannot be used. We can determine Mammal IS-A Animal by using the instanceof operator


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 running results of the above example are as follows:

true
true
true

After introducing the

extends

keyword, let’s take a look at how the implements keyword is used to represent the IS-A relationship.

ImplementsThe keyword is used when a class inherits an interface. The keyword extends cannot be used in this case.

Instance

public interface Animal {}

public class Mammal implements Animal{
}

public class Dog extends Mammal{
}

instanceof keyword

You can use the instanceof operator to check whether 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 running results of the above example are as follows:

true
true
true

HAS-A relationship

HAS-A represents the affiliation between a 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 class and the Speed ​​class have a HAS-A relationship (Van has a Speed), so there is no need to paste all the code of the Speed ​​class into the Van class, and the Speed ​​class Can also be reused for multiple applications.

In the object-oriented feature, users do not have to worry about how the internal implementation of the class is implemented.

The Van class hides the implementation details from users. Therefore, users only need to know how to call the Van class to complete a certain function, and do not have to know whether the Van class does it itself or calls other classes to do these tasks. .

Java only supports single inheritance, that is, a class cannot inherit multiple classes.

The following approach is illegal:

public class extends Animal, Mammal{}

Java only supports single inheritance (inheriting basic classes and abstract classes), but we can use interfaces to implement it (multiple inheritance interfaces to implement), The script structure is as follows:

public class Apple extends Fruit implements Fruit1, Fruit2{}

Generally, we use the extends keyword to inherit basic classes and abstract classes, and use the implements keyword to inherit interface classes.