Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java inheritance


May 10, 2021 Java


Table of contents


Java inheritance

Inheritance is an integral part of all OOP and Java languages.

Inheritance is a cornerstone of Java's object programming technology, one of the three characteristics of facing objects, and an important means to realize software reusability.

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

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

The use of these two keywords determines whether one object and the other are IS-A (one) relationship.

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

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

The extends keyword allows you to state that one class inherits another, 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 {
    public int z;

    public void fund(){

    }
    
}

The code snippet above shows that class B is inherited from class A, which is a sub-class of class A. C lass A, on the other, is a sub-class of Object, which can be declared without display.

As a subsection, an instance of class B has all the member variables of class A, but does not have access to class B, a member variable of the private type, which guarantees the encapsulation of class A.


IS-A relationship

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

Here's how to implement 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 example above, the following statement is correct:

  • The Animal class is the parent of the Animal class.
  • The Animal class is the parent of the Reptile class.
  • The Process and Reptile classes are sub-classes of the Animal class.
  • The Dog class is both a sub-class of the System class and a sub-class of the Animal class.

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

  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal

So: Dog IS-A Animal

By using the keyword extends, children can inherit all but the private properties of the parent class.

By using the instanceof operator, we were able to determine the Is-A Animal of The Process

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 above examples compile and run as follows:

true
true
true

After introducing the extends keyword, let's look at how the implements keyword is used to represent the IS-A relationship.

The Implements keyword is used in the case of class inheritance interfaces, in which case the keyword extends cannot be used.

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 Process 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 above examples compile and run as follows:

true
true
true

HAS-A relationship

HAS-A represents the dependent relationship between a class and its members. This helps with 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 are HAS-A relationships (Van has a Speed), so that you don't have to paste all the code of the Speed class into the Van class, and the Speed class can be reused in multiple applications.

In object-oriented properties, users don't have to worry about how the inside of the class is implemented.

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 accomplish a function, not whether the Van class does it itself or calls other classes to do the work.

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

The following is illegal:

public class extends Animal, Mammal{} 

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

public class Apple extends Fruit implements Fruit1, Fruit2{}

In general, we inherit basic and abstract classes with the extends keyword, and implement the interface class with the implements keyword.