You are on page 1of 1

Lesson 3: Inheritance and Polymorphism

I. Terminologies
a. Inheritance –
o The mechanism in Java that allows extending the definition of a class
without making any physical changes to the existing class.
o “is-a” relationship.
o Inheritance relationship enables a subclass to inherit features from its
superclass. Furthermore, the subclass can add new features of its own.
b. Super class – or base class; existing class.
c. Sub-class – or the derived class; the new class created from an existing class.
d. Polymorphism – associating multiple meanings with the same method name.
e. Method overriding – a method which redefines the method of the superclass; same
signature and the same return type as in its superclass.
f. Method overloading – the corresponding method in a class has the same name but
different parameter lists.

II. Sample Program

public class Rectangle public class Square extends


{ Rectangle
public double length, width; {
public Square(double l,
public Rectangle(double double w)
length, double width) {
{ super(l,w);
this.length=length; }
this.width=width;
} public double area()
public double getLength() {
{ return length*length;
return length; }
} public double perimeter()
public double getWidth() {
{ return 4*length;
return width; }
} }
public double area()
{
return length*width;
} Rectangle
public double perimeter()
{
return (2*length)
+(2*width);
}
}
Square

New Era University


Computer Programming 2 Page 1

You might also like