Lab 10
Lab 10
Lab Manual
OOP
2nd- Lab-10: Polymorphism and Abstract Class
Semeste
Laboratory 10:
Statement Purpose:
At the end of this lab, the students should be able to understand:
Method Overloading
Method Overriding
Polymorphism
Abstract Classes
Method Overloading:
If a class has multiple methods having same name but different in parameters, it is called Method
overloading.
public class Calculation {
int addition(int i, int j)
{
return i + j ;
}
String addition(String s1, String s2)
{
return s1 + s2;
}
double addition(double d1, double d2)
{
return d1 + d2;
}
}
public class AddOperation {
public static void main(String[] args) {
Calculation cal = new Calculation();
System.out.println(cal.addition(1,2));
System.out.println(cal.addition("Hello ","World"));
System.out.println(cal.addition(1.5,2));
}
}
Method Overriding:
In a class hierarchy, when a method in a subclass has the same name and type signature as a method
in its superclass, then the method in the subclass is said to override the method in the superclass.
When an overridden method is called from within a subclass, it will always refer to the version of that
method defined by the subclass. The version of the method defined by the superclass will be hidden.
NOTE: Don't confuse the term overriding with the term overloading, overriding means inheriting a
method from the super class and altering its functionality, while overloading means having
constructors and methods with the same name but with different signatures.
Example:
Output:
K : 3
If you want to access the superclass version of an overridden method, you can do so by using super. If
the method in B is changed as shown below, the superclass version of show() is called within the
subclass version. This allows all instance variables to be displayed.
void show()
{
super.show();
System.out.println("K : " + k);
}
Output:
i and j: 1 2
K : 3
Polymorphism:
Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
Method overriding is a feature which you get when you implement inheritance in your program.
//3rd class
public class Cat extends Animal {
public void makeNoise()
{
System.out.println("Meawoo");
}
}
Now which makeNoise() method will be called, depends on type of actual instance created on runtime
e.g.
Java uses a technique for method invocation called "dynamic dispatch". If I have
class A {
public void draw()
{
System.out.println("draw in A class");
}
public void spin()
{
System.out.println("spin in A class");
}
}
class B extends A {
public void draw()
{
System.out.println("draw in B class");
}
Bilal Shahnawaz Lab-10 3
2nd- Lab-10: Polymorphism and Abstract Class
Semeste
public void bad()
{
System.out.println("bad in B class");
}
}
class Lab10 {
public static void main(String[] args) {
A testObject = new B();
testObject.draw(); // calls B's draw, polymorphic
testObject.spin(); // calls A's spin, inherited by B
testObject.bad(); // compiler error, you are manipulating this as an A’s
methods
}
}
Output:
Then we see that B inherits spin from A. However, when we try to manipulate the object as if it were
a type A, we still get B's behavior for draw. The draw behavior is polymorphic.
Abstract Method: Abstract method does not have any body. It always ends with (;) semicolon.
Abstract method must be overridden. It must be in an abstract class. It can never be static and final.
Abstract methods are those which need to be implemented in subclass. If class has one abstract
method then whole class is declared as abstract. Private method cannot be abstract.
Output:
Sum of Addition: 13
Sum of Subtraction: 16
Sum of Multiplication: 8
Abstract Class:
figref=t;
System.out.println("Area is " +figref.area());
}
}
Output:
Question 1: Marks:5
Make an abstract class named Shape with two abstract methods area and perimeter. Implement a
subclass “EquilateralTriangle” having a double variable side denoting the three sides of the equilateral
triangle [Note that since all the 3 sides are equal, the constructor will have only one parameter]. The
area and perimeter of the equilateral triangle are given as follows:
√3 2
Area= side
4
Perimeter = 3*side
Test your class using the main class TestShapes and calculate the area and perimeter of
EquilateralTriangle.
Question 2: Marks: 5
Consider a scenario where Bank is a class that has an abstract method of getRateOfInterest, but rate of
interest varies according to banks. For example, HBL and ABL banks can provide 7% and 8% rate of
interest. Make a class TestBank where reference variable of Bank class is used to show the rate of
interest of both banks.