|
|
| Inheritence
|
|
"As the name inheritance suggests an object is able to inherit characteristics from another object." In more concrete terms,
an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have
characteristics in common with each other.
As you've already learned, objects define their interaction with the outside world through the methods that they expose.
Methods form the object's interface with the outside world; the buttons on the front of your television set, for example,
are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power"
button to turn the television on and off.
In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior,
if specified as an interface, might appear as follows:
|
interface Bicycle {
void changeCadence(int newValue); // wheel revolutions per minute
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example,
such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {
// remainder of this class implemented as before
}
Implementing an interface allows a class to become more formal about the behavior it promises to provide.
Interfaces form a contract between the class and the outside world, and this contract is enforced at build
time by the compiler. If your class claims to implement an interface, all methods defined by that interface
must appear in its source code before the class will successfully compile.
|
What Is a Superclass?
In the relationship between two objects a superclass is the name given to the class that is
being inherited from. It sounds like it's a super duper class but remember that it's the more generic version. Better names
to use might be base class or simply parent class.
To take a more real world example this time we could have a superclass called "Person". Its state holds the person's name,
address, height and weight and has behaviors like go shopping, make the bed, and watch TV. We could make two new classes
that inherit from Person called "Student" and "Worker". They are more specialized versions because although they have names,
addresses, watch TV and go shopping, they also have characteristics that are different from each other. Worker could have a
state that holds a job title and place of employment whereas Student might hold data on an area of study and an institution
of learning.
|
What Is a Subclass?
In the relationship between two objects a subclass is the name given to the class that
is inheriting from the superclass. Although it sounds a little more drab, remember that it's a more specialized version
of the superclass. Subclasses can also be known as derived classes or simply child classes.
In the previous example, Student and Worker are the subclasses.
|
Example:
public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}
Now based on the above example, In Object Oriented terms following are true:
* Animal is the superclass of Mammal class.
* Animal is the superclass of Reptile class.
* Mammal and Reptile are sub classes of Animal class.
* Dog is the subclass of both Mammal and Animal classes.
Now if we consider the IS-A relationship we can say:
* Mammal IS-A Animal
* Reptile IS-A Animal
* Dog IS-A Mammal
* Hence : Dog IS-A Animal as well
Code:
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);
}
}
Using Interface:
interface Animal{}
class Mammal implements Animal{}
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);
}
|
|