StudentName: M Sarmad Iqbal RegistrationNo: cosc222102008
LAB 09 Polymorphism and Super keyword in Java
Lab Objectives:
• Understanding the concept of polymorphism
• Understand the super keyword in inheritance
Software Required:
Netbeans IDE
super Keyword in Java
Sample Code:
class Box { private
double
width;
private
double
height;
private
double
depth;
// construct clone of an object
Box(Box ob) { // pass object to
constructor width =
ob.width; height =
ob.height;
depth = ob.depth;
}
// constructor used when all dimensions
specified Box(double w, double h,
double d) { widt
h =
w;
heig
ht =
h;
dept
h =
d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to
indicate height = -1; // an
uninitialized depth = -1;
// box
}
// constructor used when cube is
created Box(double len) { width =
height = depth = len;
}
// compute and return volume
double volume()
{ return width * height * depth;
}
}
// BoxWeight now fully implements all
constructors. class BoxWeight extends Box
{ double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object
to constructor super(ob); weight =
ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d,
double m) { super(w, h, d); // call superclass
constructor weight = m;
}
// default constructor
BoxWeight() { super()
;
weight
= -1;
}
// constructor used when cube is
created BoxWeight(double len,
double m) { super(l en); weight
= m;
}
}
class DemoSuper { public static void
main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4,
0.076); BoxWeight mybox3 = new BoxWeight();
// default BoxWeight mycube = new
BoxWeight(3, 2); BoxWeight myclone =
new BoxWeight(mybox1); double vol; vol =
mybox1.volume();
System.out.println("Volume of mybox1 is " +
vol);
System.out.println("Weight of mybox1 is " +
mybox1.weight); System.out.println(); vol =
mybox2.volume();
System.out.println("Volume of mybox2 is " +
vol);
System.out.println("Weight of mybox2 is " +
mybox2.weight); System.out.println(); vol =
mybox3.volume();
System.out.println("Volume of mybox3 is " +
vol);
System.out.println("Weight of mybox3 is " +
mybox3.weight); System.out.println(); vol =
myclone.volume();
System.out.println("Volume of myclone is " +
vol);
System.out.println("Weight of myclone is "
+ myclone.weight); System.out.println(); vol
= mycube.volume();
System.out.println("Volume of mycube is " +
vol);
System.out.println("Weight of mycube is " +
mycube.weight); System.out.println();
}
}
TASK 1: super keyword is used to refer immediate parent class
instance variable.
We can use super keyword to access the data member or field of parent class. It is used if
parent class and child class have same fields. Perform following steps
• Create a class and its sub class both having same data member.
• Use super in child class to print the value of parent class variable.
• create a test class to implement main method with demo functionality
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black"; void
printColor(){
System.out.println(color);
System.out.println(super.color);
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog(); d.printColor();
}} Output
black
white
TASK 2: super Keyword can be used to invoke parent class method.
The super keyword can also be used to invoke parent class method. It should be used if
subclass contains the same method as parent class. In other words, it is used if method is
overridden. Perform following steps
• Create a class animal with a method eat() which prints "eating"
• Create subclass of animal class with method bark() which prints "barking" and method eat()
same name as super class but it prints "eating meat"
• Write a print() method in subclass which call all the super and sub class methods.
Program: class
Animal
{
String eat;
void eat()
{
System.out.println("Eating");
}}
class Dog extends
Animal{ String Bark;
void printbark()
{
System.out.println("Barking");
}
String eat;
void printeat()
{
System.out.println("Eating meat");
}
public class Main {
public static void main(String[] args)
{ Animal obj = new Animal();
obj.eat();
Dog obj2=new Dog();
obj2.printbark(); obj2.printeat();
}
}
Output:
Eating
Barking
Eating meat
TASK 3: super Keyword is used to invoke parent class constructor.
The super keyword can also be used to invoke the parent class constructor. Create a scenario
in which above use of the super keyword is depicted.
Scenario:
Polymorphism in Java
Polymorphism in java is a concept by which we can perform a single action by different ways.
Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
Usage of Java Method Overriding
Method overriding is used to provide specific implementation of a method that is already provided by
its super class.
Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• method must have same name as in the parent class •
method must have same parameter as in the parent class.
• must be IS-A relationship (inheritance).
Sample Code:
class Vehicle{ void run()
{System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running safely");}
public static void
main(String args[]){ Bike2
obj = new Bike2(); obj.run();
}
Task 05: Consider a scenario, Bank is a class that provides functionality to
get rate of interest. But, rate of interest varies according to banks. For
example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate
of interest.
class Bank{ float getRateOfInterest(){return 0;}
}
class SBI extends Bank{ float
getRateOfInterest(){return 8 ;}
}
class ICICI extends Bank{
float getRateOfInterest(){return 7 ;}
}
class AXIS extends Bank{
float getRateOfInterest(){return 9;}
}
class Test2{
public static void main(String args[]){
SBI obj1=new SBI();
ICICI obj2=new ICICI();
AXIS obj3=new AXIS();
System.out.println("SBI Rate of Interest: "+obj1.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+obj2.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+obj3.getRateOfInterest());
}
}
Output:
SBI Rate of Interest: 8.0
ICICI Rate of Interest: 7.0
AXIS Rate of Interest: 9.0