The following is referenced from http://wiki.jikexueyuan.com/project/java/inheritance.html:
Inheritance can be defined as the process by which an object obtains another object property. Using inheritance allows information to be managed in an orderly manner in an inherited order.
When talking about inheritance, the most commonly used keywords should be extends and implements. These keywords will determine whether an object is a type a or another type. By using these keywords, you can make one object get the properties of another object.
First, is-a relations
Is-a is saying: This object belongs to another object. The following is a keyword extends to implement inheritance.
Public class animal{} Public class extends animal{} Public class extends animal{} Public class extends mammal{}
Now, based on the above example, in object-oriented programming, follow these points:
- Animal is the parent class of the mammal class.
- Animal is the parent class of the reptile class.
- Reptile and mammal are subclasses of the animal class.
- Dog is a subclass of the mammal class and the animal class.
Now, for the is-a relationship, you can say:
- Mammal is a animal
- Reptile is a animal
- Dog is a mammal
- Dog is a animal.
With the keyword extends, subclasses can inherit all the properties of the parent class in addition to the private property.
Using the instance operation ensures that the mammal is actually a animal.
Example:
classanimal{}classMammalextendsanimal{}classReptileextendsanimal{} Public classDogextendsMammal { Public Static voidMain (String args[]) {Animal a=NewAnimal (); Mammal M=Newmammal (); Dog D=NewDog (); System.out.println (MinstanceofAnimal); System.out.println (dinstanceofmammal); System.out.println (dinstanceofAnimal); }}//This will give you the following results:truetruetrue
Already well understood the extends keyword, next see implements keyword is how to establish is-a relationship.
The implements keyword is used when the class inherits the interface. An interface cannot be inherited by a class using extends .
Implements can also inherit multiple interfaces.
Example:
Public Interface Animal {} Public class Implements animal{} Public class extends mammal{}
Second, the key word instanceof
Using the instanceof operator to check to determine whether mammal is actually animal,dog is actually a animal.
Interfaceanimal{}classMammalImplementsanimal{} Public classDogextendsmammal{ Public Static voidMain (String args[]) {mammal M=Newmammal (); Dog D=NewDog (); System.out.println (MinstanceofAnimal); System.out.println (dinstanceofmammal); System.out.println (dinstanceofAnimal); }} //this will produce the following results:truetruetrue
Iii. has-a Relations
The relationship is based on ease of use. This determines whether a class contains some properties of a. This relationship helps reduce the occurrence of code duplication and vulnerabilities.
Look at the following example:
Public class vehicle{} Public class speed{} Public class extends vehicle{ Private
This example shows that van contains speed. Because the speed class is defined separately, you do not have to add the entire speed class's code to the Van class, allowing it to reuse the speed class in multiple applications.
In object-oriented, the user does not have to consider which object is doing the actual work. To implement this function, the van class hides to the user the classes that implement the specifics. When the user asks van to do a job, the van class either does it himself or asks for other classes to do the work.
Keep in mind a very important fact that Java only supports single inheritance, which means that a class can inherit only one class, so the following is illegal :
public class extends Animal, mammal{}
However, a class can implement one or more interfaces, which allows Java to get rid of problems that cannot be inherited more.
Test Project: https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test17
Inheritance in Java