Saturday, 27 April 2013

Inheritance

Inheritance
Many Objects often have many things common with each other. They share same properties or their behavior is same.

For example: Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, and current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets








of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Let’s understand this by defining classes:
A class declaration for a Bicycle-
public class Bicycle {

// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}

public void setGear(int newValue) {
gear = newValue;
}

public void applyBrake(int decrement) {
speed -= decrement;
}

public void speedUp(int increment) {
speed += increment;
 
A class declaration for a MountainBike -

public class MountainBike extends Bicycle {
// the MountainBike subclass adds one field
public int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}     
// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}     }


A class declaration for a MountainBike-
public class MountainBike extends Bicycle {
// the MountainBike subclass adds one field
public int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}     
// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
} }



Inheritance can be defined as the process where one object acquires the properties of another.

When we talk about inheritance the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

IS-A Relationship:
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

In the above example MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug. Here we can say MountainBike IS-A Bicycle.

Similarly we can define
public class RoadBike extends Bicycle{
}
Now based on the above example, In Object Oriented terms following are true:

  • Bicycle is the superclass of MountainBike class.
  • Bicycle is the superclass of RoadBike class.
  • MountainBike and RoadBike are sub classes of Bicycle class.

Another Example of Inheritance

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.


































No comments:

Post a Comment