0% found this document useful (0 votes)
23 views8 pages

Lab 10

The document discusses object-oriented programming concepts including method overloading, method overriding, polymorphism, and abstract classes. It provides examples of each concept and how they work in Java.

Uploaded by

Hamza Ramay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Lab 10

The document discusses object-oriented programming concepts including method overloading, method overriding, polymorphism, and abstract classes. It provides examples of each concept and how they work in Java.

Uploaded by

Hamza Ramay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NUML

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:

//create first class


public class Override1 {
int i,j;
Override1(int a, int b)
{
i=a;
j=b;
Bilal Shahnawaz Lab-10 1
2nd- Lab-10: Polymorphism and Abstract Class
Semeste
}
//display i and j
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}

//create second class that inherits first class


public class Override2 extends Override1{
int k;
Override2(int a, int b, int c)
{
super(a, b);
k = c;
}
//display k -this overrides show() in A
void show()
{
System.out.println("K : " + k);
}
}

//Create a 3rd class with main method


public class OverrideTesting {
public static void main(String[] args) {
Override2 Obj = new Override2(1, 2, 3);
Obj.show();
}
}

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.

In java language, polymorphism is essentially considered into two versions.

1. Compile time polymorphism (static binding or method overloading)


2. Runtime polymorphism (dynamic binding or method overriding)
Bilal Shahnawaz Lab-10 2
2nd- Lab-10: Polymorphism and Abstract Class
Semeste
A simple example can be from real world e.g. Animal. An application can have Animal class, and its
specialized sub classes like Cat and Dog. These subclasses will override the default behavior provided
by Animal class + some of its own specific behavior.

//make first class


public class Animal {
public void makeNoise()
{
System.out.println("Some sound");
}
}

//make second class


public class Dog extends Animal {
public void makeNoise()
{
System.out.println("Bark");
}
}

//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.

//4th class with main method


public class PolymorphismDemo {
public static void main(String[] args) {
Animal a1 = new Cat();
a1.makeNoise(); //Prints Meowoo
Animal a2 = new Dog();
a2.makeNoise(); //Prints Bark
}
}

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.

Example: This example shows how abstract method is used in a class.

public abstract class Calculator {


//define 2 integers
protected int no1;
protected int no2;
//declare abstract method
abstract int sum();
}

public class Addition extends Calculator {


Addition (int n1, int n2)
{
no1 = n1;
no2 = n2;
}
int sum()//define method
{
return no1 + no2;//return Addition
}
}

public class Subtraction extends Calculator {


Subtraction (int n1, int n2)
{
no1 = n1;
no2 = n2;

Bilal Shahnawaz Lab-10 4


2nd- Lab-10: Polymorphism and Abstract Class
Semeste
}
int sum()//define method
{
return no1 - no2;//return Subtraction
}
}

public class Multiplication extends Calculator {


Multiplication (int n1, int n2)
{
no1 = n1;
no2 = n2;
}
int sum()//define method
{
return no1 * no2;//return Multiplication
}
}

public class CalculatorDemo {


public static void main(String[] args) {
Addition a = new Addition (5, 8);
Subtraction s = new Subtraction (32, 16);
Multiplication m = new Multiplication (4, 2);
System.out.println ("Sum of Addition: " + a.sum ());
System.out.println ("Sum of Subtraction: " + s.sum ());
System.out.println ("Sum of Multiplication: " + m.sum ());
}
}

Output:
Sum of Addition: 13
Sum of Subtraction: 16
Sum of Multiplication: 8

Abstract Class:

//Using abstract methods and classes.

public abstract class Figure {


double dim1;
double dim2;
Figure(double a, double b){
dim1=a;
dim2=b;
}
//area is now an abstract method
abstract double area();
}

public class Rectangle extends Figure{


Rectangle(double a, double b)
{
super(a, b);
}
//override area for rectangle
double area(){
Bilal Shahnawaz Lab-10 5
2nd- Lab-10: Polymorphism and Abstract Class
Semeste
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}

public class Triangle extends Figure{


Triangle(double a, double b){
super(a, b);
}
//override area for right triangle
double area(){
System.out.println("Area for Triangle.");
return dim1 * dim2 /2;
}
}

public class AbstractAreas {


public static void main(String[] args) {
//Figure f=new Figure(10, 10); //illegal now
Rectangle r=new Rectangle(10, 8);
Triangle t=new Triangle(10.0, 8.0);

Figure figref; //this is OK, no object is created


figref=r;
System.out.println("Area is " + figref.area());

figref=t;
System.out.println("Area is " +figref.area());
}
}

Output:

inside Area for Rectangle.


Area is 80.0
Inside Area for Triangle.
Area is 40.0

Lab Task: Marks: 10

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.

Bilal Shahnawaz Lab-10 6


2nd- Lab-10: Polymorphism and Abstract Class
Semeste

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.

Bilal Shahnawaz Lab-10 7

You might also like