Saturday, December 5, 2009

Inheritance in Bluej for icse schools

This is a mechanism by which one object acquires the properties of another object of different class. Inheritance is probably the most powerful feature of object oriented programming. Inheritance is the process of creating new classes, called derived or sub classes from existing classes. The existing classes are called base or super classes. In OOP, the concept of inheritance in object oriented programming provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one.

Inheritance in object oriented programming is accomplished by using the keyword extends,

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 relationship we can say:

Mammal is an Animal

Reptile is an Animal

Dog is an Mammal

Hence : Dog is an Animal as well

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner