The best Java inheritance Tutorial In 2024, In this tutorial you can learn IS-A relationship,instanceof keyword,HAS-A relationship,
Inheritance is a cornerstone of the object-oriented programming java technology, because it allows the creation of hierarchical level classes. Inheritance can be understood as an object to get the properties from another object of the process.
If the parent class is a class A class B, class B and class C is the parent class, and we also call C is a subclass of A, Class C from Class A is inherited. In Java, class inheritance single inheritance, that is, a subclass can have only one parent class
Two most commonly used keywords inheritance is extends and implements.
Both the use of keywords and determine whether an object is another object IS-A (a) relationship.
By using these two keywords, we can achieve an object to get the properties of another object.
All Java classes are inherited by the java.lang.Object class, so the class Object is the ancestor of all classes, and in addition Object, all classes must have a parent class.
By over-extends keywords can declare a class inherits from another class, 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, B inherits from A, B is a subclass of A. And A is a subclass of Object, there may not be explicitly declared.
As a subclass instance B of A has all the member variables, but for members of the private variable B, but do not have access, which guarantees encapsulation A's.That IS-A: An object is a another object classification.
Here is the keyword extends implementation inheritance.
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:
Analysis of IS-A relationship in the example above, as follows:
Therefore: Dog IS-A Animal
By using the keyword extends, subclasses inherit all the parent class methods and properties, but can not use private (private) methods and properties.
We use the instanceof operator can determine Mammal IS-A Animal
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 compiled results are as follows:
true true true
After introducing extends keyword, we'll look at how to use the implements keyword to indicate the IS-A relationship.
Implements keyword in the case of class inheritance interface, this situation can not use the keyword extends.
public interface Animal {} public class Mammal implements Animal{ } public class Dog extends Mammal{ }
You can use the instanceof operator to test whether Mammal and dog object is an instance of class Animal.
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 compiled results are as follows:
true true true
HAS-A affiliation on behalf of the class and its members. This helps to reuse code and reduce code errors.
public class Vehicle{} public class Speed{} public class Van extends Vehicle{ private Speed sp; }
Van class and Speed class is HAS-A relationship (Van has a Speed), so you will not have all the code is pasted into the Van Speed class in the class, and Speed class may be reused in multiple applications.
In object-oriented features, the user need not worry about how the internal classes.
Van class implementation details hidden from the user, so users only need to know how to call Van class to complete a certain function, without having to know Van class is to do it themselves or call other class to do the work.
Java supports only single inheritance, meaning that a class can not inherit multiple classes.
The following practice is not legal:
public class extends Animal, Mammal{}
Java supports only single inheritance (inheritance and abstract base class), but we can use the interface to implement (multiple-inheritance interfaces to achieve), the script structure such as:
public class Apple extends Fruit implements Fruit1, Fruit2{}
Generally, we inherit the base class and abstract class with the extends keyword implements the interface with the implements keyword class inheritance.