Page 134 - Computer science 868 Class 12
P. 134
public static void main()
{
rectangle ob = new rectangle(5,2);
ob.calculate();
ob.display();
}
}
In the above example, the class rectangle is inherited from the class shape. All the properties of the class shape are
included in the class rectangle.
Essential terms Related to Inheritance
• Superclass: The class from where features are inherited to another class is called a superclass. Superclass
It is also known as base class or parent class. (In the above program, shape is the superclass.)
Shape
• Subclass: The class which inherits the features of another class is called a subclass. It is also
called the derived class or extended class or child class. Apart from the inherited features and Subclass
behaviour of the base class, it may also have its own data members and methods. (In the above
program, rectangle is the subclass.) Rectangle
4.4.4 Polymorphism
The word polymorphism comes from the Greek vocabulary meaning “having many forms” ("poly" means many and
"morphe" means form). It is the ability how the same variables are processed in one or more forms.
Polymorphism is the capability of a reference variable to alter its characteristics and behaviour according to what
object it is having.
Definition
In object-oriented programming, polymorphism is the feature of being able to allot a dissimilar meaning
so that a variable, a method or an object can have more than one form.
Let us see the programming implementation of polymorphism with the help of the following example:
class overloading
{
double volume(double r)
{
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r)
{
double vol = 22/ (double)7 * Math.pow(r, 2) * h;
return vol;
}
double volume(double l, double b, double h)
{
double vol = l * b * h;
return vol;
}
}
Here, the method volume() is overloaded three times and each time different parameters are used depending on the
requirement.
132132 Touchpad Computer Science-XII

