The first major characteristic of an object-oriented program is encapsulation. The second one is inheritance. Let's now have a look at it.
Having bought an old house to live, the person may try to reconstruct it in order to have it extended into another one, more modern and more comfortable without ruining the old version in general. Thus after reconstruction the house will be a subclass the house that already existed. The new house inherits from the existing one.
The same with the OO program: a class can normally inherit the attributes and characteristics of another class. This mechanism can be blocked by using different ways, though.
The original class is often called the base class or the superclass, and the new class is often called the derived class or the subclass. Inheritance is often referred to as extending the base class or superclass.
Inheritance is hierarchical. In other words, a class may be the subclass of one class and the superclass of another class.
The derived class inherits the data representation and behaviour of the base class except where the derived class modifies the behaviour by overriding methods. The derived class can also add new data representation and behaviour that is unique to its own purpose.
A program can usually instantiate objects of a base class as well as of a class which is derived from the base class. It is also possible to block instantiation of the base class in some cases by defining it as an abstract base class. If the base class is an abstract base class -- one that exists only to be derived from - the program may not instantiate objects of the base class but can instantiate objects of classes derived from the base class.
The JAVA inheritance mechanism allows to build an orderly hierarchy of classes.
When several of your abstract data types have characteristics in common, you can design their commonalities into a single base class and separate their unique characteristics into unique derived classes. This is one of the purposes of inheritance.
Remember, we have created a class Human, but not all humans are the same, but different: we have different race, sex, culture, religion etc. Nonetheless we have some very common features: two legs, two arms, one head. We all can work, sleep, etc. Developed class Human has methods to indicate a human state concerning possibility of working. This is the common parameter therefore of the Human class we have built.
From this base class, we may derive a Gentleman class and a Lady class. They certainly have different rest: the Lady prefers to drink tea with her neighbours but the Gentleman largely his pint of beer in his favourite pub.
Objects of the classes will then be able to deal with all Human parameters as well as new ones.
You may have noticed that in this hierarchical class structure, inheritance causes the structure to grow in a direction from most general to less general. This is typical.
Here you are an example. In the next fragment of the code one line of the code is essential: it describes the Lady and Gentleman subclasses:
class Lady extends Human {
...
}
class Gentleman extends Human {
...
}
The keyword extends shows the Lady and Gentleman classes are subclasses of the Human class.
Now we might have a look at the next fragment of the code, that was replaced ....
// a new method
public void drinking(){
System.out.println
("Drinking tea with my neighbors");
}
In this part of the program we declared a new method, drinking. Of course, the very similar method the Gentleman has, but about beer!
Yet another sensitive fragment of the code shows manipulating with method declared in the base class, Human. The keyword super is used to access that method following by period, or dot operator.
// changing mathod getHumanInfo
public String getHumanInfo() {
String info;
info=super.getHumanInfo();
info=info+"and I am a lady.";
return info;
}
The last fragment of the code shows how to use new classes. Nothing new, isn't it?
... Gentleman me = new Gentleman(); Lady anne = new Lady(); me.setPerson (0, "Olexiy", "Ukrainian"); //store data you.setPerson(0, "Anne", "Russian"); //store data System.out.println( me.getHumanInfo() ); System.out.println( you.getHumanInfo() ); ...