Module 8 OOP
Module 8 OOP
Chapter 8
OOP Polymorphism
Introduction
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Learning Outcomes:
If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.
If performing only one operation, having same name of the methods increases the
readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number
of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you as well as other programmers to understand the
behavior of the method because its name differs.
NOTE: Method Overloading is not possible by changing the return type of the method only.
Object-Oriented Programming
In this example, two methods were created, first add() method performs addition
of two numbers and second add method performs addition of three numbers.
1.
2.
3. Output of the Program:
4.
5. 22
33
6.
7.
8.
9.
In this example, we have created two methods that differs in data type. The first
add method receives two integer arguments and second add method receives two double
arguments.
1. 1. class Adder{
2. 2. static int add(int a, int b){return a+b;}
3. 3. static double add(double a, double b)
{return a+b;} Output of the Program:
4. 4. }
22
5. 5. class TestOverloading2{
24.9
6. 6. public static void main(String[] args){
7. 7. System.out.println(Adder.add(11,11));
8. 8. System.out.println(Adder.add(12.3,12.6));
9. 9. }}
QUESTION: Why Method Overloading is not possible by changing the return type of method only?
ANSWER: In java, method overloading is not possible by changing the return type of the method only
because of ambiguity.
Example:
1. 1. class Adder{
2. 2. static int add(int a,int b){return a+b;}
3. 3. static double add(int a,int b){return a+b;}
4. 4. }
5. 5. class TestOverloading3{
Object-Oriented Programming
1. 1. class TestOverloading4{
2. 2. public static void main(String[] args){System.out.println("main with String[]");}
3. 3. public static void main(String args){System.out.println("main with String");}
4. 4. public static void main(){System.out.println("main without args");}
5. 5. }
As displayed in the above diagram, byte can be promoted to short, int, long, float or
double. The short datatype can be promoted to int, long, float or double. The char datatype can be
promoted to int, long, float or double and so on.
Example:
1. class OverloadingCalculation1{
2. void sum(int a,long b){System.out.println(a+b);}
1. 3. void sum(int a,int b,int c){System.out.println(a+b+c);}
2. 4.
3. 5. public static void main(String args[]){
4. 6. OverloadingCalculation1 obj=new OverloadingCalculation1();
5. 7. obj.sum(20,20);//now second int literal will be promoted to long
6. 8. obj.sum(20,20,20);
7. 9.
8. 10. }
11. }
If there are matching type arguments in the method, type promotion is not performed.
1. class OverloadingCalculation2{
1. 2. void sum(int a,int b){System.out.println("int arg method invoked");}
3. void sum(long a,long b){System.out.println("long arg method invoked");}
2. 4.
5. public static void main(String args[]){
3. 6. OverloadingCalculation2 obj=new OverloadingCalculation2();
7. obj.sum(20,20);//now int arg sum() method gets invoked
4. 8. }
5. 9. }
ASSESSMENT TASK
METHOD OVERRIDING
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java. In other words, if a subclass provides the specific implementation
of the method that has been declared by one of its parent class, it is known as method overriding.
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
Let's understand the problem that we may face in the program if we don't use method
overriding.
Object-Oriented Programming
1. //Java Program to demonstrate why method overriding is needed
2. //Here, we are calling the method of parent class with child
3. //class object.
4. //Creating a parent class
5. class Vehicle{
6. void run(){System.out.println("Vehicle is running");}
7. }
8. //Creating a child class Output of the Program:
9. class Bike extends Vehicle{
Vehicle is running
10. public static void main(String args[]){
11. //creating an instance of child class
12. Bike obj = new Bike();
13. //calling the method with child class instance
14. obj.run();
15. }
16. }
In this example, run method is defined in the subclass as defined in the parent class but it
has some specific implementation. The name and parameter of the method are the same, and there
is IS-A relationship between the classes, so there is method overriding.
1. //Java Program to illustrate the use of Java Method Overriding
2. //Creating a parent class.
3. class Vehicle{
4. //defining a method
5. void run(){System.out.println("Vehicle is running");}
6. }
7. //Creating a child class
8. class Bike2 extends Vehicle{ Output of the Program:
9. //defining the same method as in the parent class
Bike is running safely
10. void run(){System.out.println("Bike is running safely")
11. }
12. public static void main(String args[]){
13. Bike2 obj = new Bike2();//creating object
14. obj.run();//calling method
15. }
16. }
Object-Oriented Programming
Consider a scenario where Bank is a class that provides functionality to get the rate of
interest. However, the rate of interest varies according to banks. For example, SBI, ICICI and
AXIS banks could provide 8%, 7%, and 9% rate of interest.
1. //Java Program to demonstrate the real scenario of Java Method Overriding
2. //where three classes are overriding the method of a parent class.
3. //Creating a parent class.
4. class Bank{
5. int getRateOfInterest(){return 0;}
6. }
7. //Creating child classes.
8. class SBI extends Bank{
9. int getRateOfInterest(){return 8;}
10. } Output of the Program:
11. class ICICI extends Bank{ SBI Rate of Interest: 8
12. int getRateOfInterest(){return 7;} ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
13. }
14. class AXIS extends Bank{
15. int getRateOfInterest(){return 9;}
16. }
17. //Test class to create objects and call the methods
18. class Test2{
19. public static void main(String args[]){
20. SBI s=new SBI();
21. ICICI i=new ICICI();
22. AXIS a=new AXIS();
23. System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
24. System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
25. System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
26. }
27. }
Object-Oriented Programming
ASSESSMENT TASK
class Human{
public void eat()
{
System.out.println("Human is eating");
} Output of the Program:
}
class Boy extends Human{
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
obj.eat();
}
}
class MyBaseClass{
protected void disp()
{
System.out.println("Parent class method");
} Output of the Program:
}
class MyChildClass extends MyBaseClass{
public void disp(){
System.out.println("Child class method");
}
public static void main( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
Object-Oriented Programming
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference 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.
Example:
black
white
Object-Oriented Programming
In the above example, Animal and Dog both classes have a common property color. If we
print color property, it will print the color of current class by default. To access the parent
property, we need to use super keyword.
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.
Example:
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating bread...");}
6. void bark(){System.out.println("barking...");} Output of the Program:
7. void work(){
eating…
8. super.eat(); barking…
9. bark();
10. }
11. }
12. class TestSuper2{
13. public static void main(String args[]){
14. Dog d=new Dog();
15. d.work();
16. }}
In the above example Animal and Dog both classes have eat() method if we call eat()
method from Dog class, it will call the eat() method of Dog class by default because priority is
given to local. To call the parent class method, use the super keyword.
Example 1:
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
Output of the Program:
5. Dog(){
6. super(); animal is created
7. System.out.println("dog is created"); dog is created
8. }
9. }
10. class TestSuper3{
Object-Oriented Programming
NOTE: super() is added in each class constructor automatically by compiler if there is no super()
or this().
Let's see the real use of super keyword. Here, Emp class inherits Person class so all the
properties of Person will be inherited to Emp by default. To initialize all the property, we are
using parent class constructor from child class. In such way, we are reusing the parent class
constructor.
Example 2:
1. class Person{
2. int id;
3. String name;
4. Person(int id,String name){
5. this.id=id;
6. this.name=name; Output of the Program:
7. }
1 ankit 45000
8. }
9. class Emp extends Person{
10. float salary;
11. Emp(int id,String name,float salary){
12. super(id,name);//reusing parent constructor
13. this.salary=salary;
14. }
15. void display(){System.out.println(id+" "+name+" "+salary);}
16. }
17. class TestSuper5{
18. public static void main(String[] args){
19. Emp e1=new Emp(1,"ankit",45000f);
20. e1.display();
21. }}
Object-Oriented Programming
ASSESSMENT TASK
class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{ Output of the Program:
int num = 110;
void printNumber(){
System.out.println(num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber();
}
}
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
public void animalSound() { Output of the Program:
super.animalSound();
System.out.println("The dog says: bow wow");
}
}
Final Keyword
1. Final variable
2. Final method
3. Final class
4. Is final method inherited?
The final keyword in java is used to restrict the user. The java final keyword can be used
in many contexts. Final can be:
variable
method
class
The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.
1. Final variable. If you make any variable as final, you cannot change the value of final
variable (It will be constant).
Example:
There is a final variable speedlimit, we are going to change the value of this variable, but
It can't be changed because final variable once assigned a value can never be changed.
1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400; Output of the Program:
5. }
6. public static void main(String args[]){ Compile Time Error
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class
2. Final method. If you make any method as final, you cannot override it.
Object-Oriented Programming
Example:
1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4. class Honda extends Bike{
5. void run(){System.out.println("running safely with 100kmph");
Output of the Program:
6. }
7. public static void main(String args[]){ Compile Time Error
8. Honda honda= new Honda();
9. honda.run();
10. }
11. }
3. Final class. If you make any class as final, you cannot extend it.
Example:
Yes, final method is inherited but you cannot override it.
1. final class Bike{}
2. class Honda1 extends Bike{
3. void run(){System.out.println("running safely with 100kmph");}
4. public static void main(String args[]){
Output of the Program:
5. Honda1 honda= new Honda1();
6. honda.run(); Compile Time Error
7. }
8. }
4. Is final method inherited? Yes, final method is inherited but you cannot override it.
Example:
1. class Bike{
2. final void run(){System.out.println("running...");}
3. }
4. class Honda2 extends Bike{
Object-Oriented Programming
running…
ASSESSMENT TASK
class StudentData{
final int ROLL_NO;
StudentData(int rnum){
ROLL_NO=rnum;
}
void myMethod(){
System.out.println("Roll no is:"+ROLL_NO);
Object-Oriented Programming
class XYZ{
final void demo(){
System.out.println("XYZ Class Method");
} Output of the Program:
}
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform a single action in 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.
Object-Oriented Programming
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
Here, we will focus on runtime polymorphism in java.
Upcasting
If the reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:
Example
1. class A{}
2. class B extends A{}
1. 1. A a=new B();//upcasting
For upcasting, we can use the reference variable of class type or an interface type. For
example:
1. interface I{}
2. class A{}
3. class B extends A implements I{}
B IS-A Object
Since Object is the root class of all classes in Java, so we can write B IS-A Object.
1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{ Output of the Program:
5. void run(){System.out.println("running safely with 60km");}
6. public static void main(String args[]){ running safely with 60 km
7. Bike b = new Splendor();//upcasting
8. b.run();
9. }
10. }
Note: This example is also given in method overriding but there was no upcasting.
1. class Bank{
2. float getRateOfInterest(){return 0;}
3. }
4. class SBI extends Bank{
Object-Oriented Programming
1. class Shape{
2. void draw(){System.out.println("drawing...");}
3. }
4. class Rectangle extends Shape{
5. void draw(){System.out.println("drawing rectangle...");}
6. }
7. class Circle extends Shape{
8. void draw(){System.out.println("drawing circle...");}
9. } Output of the Program:
10. class Triangle extends Shape{
drawing rectangle...
11. void draw(){System.out.println("drawing triangle...");} drawing circle...
12. } drawing triangle...
13. class TestPolymorphism2{
14. public static void main(String args[]){
15. Shape s;
16. s=new Rectangle();
17. s.draw();
18. s=new Circle();
19. s.draw();
Object-Oriented Programming
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating bread...");}
6. }
7. class Cat extends Animal{
8. void eat(){System.out.println("eating rat...");} Output of the Program:
9. } eating bread...
10. class Lion extends Animal{ eating rat...
11. void eat(){System.out.println("eating meat...");} eating meat...
12. }
13. class TestPolymorphism3{
14. public static void main(String[] args){
15. Animal a;
16. a=new Dog();
17. a.eat();
18. a=new Cat();
19. a.eat();
20. a=new Lion();
Object-Oriented Programming
1. class Bike{
2. int speedlimit=90;
3. }
Output of the Program:
4. class Honda3 extends Bike{
5. int speedlimit=150; 90
6. public static void main(String args[]){
7. Bike obj=new Honda3();
8. System.out.println(obj.speedlimit);//90
Java Runtime Polymorphism with Multilevel Inheritance
9. }
Let's see the simple example of Runtime Polymorphism with multilevel inheritance.
1. class Animal{
2. void eat(){System.out.println("eating");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating fruits");}
6. }
Output of the Program:
7. class BabyDog extends Dog{
8. void eat(){System.out.println("drinking milk");} eating
9. public static void main(String args[]){ eating fruits
drinking Milk
10. Animal a1,a2,a3;
11. a1=new Animal();
12. a2=new Dog();
13. a3=new BabyDog();
14. a1.eat();
15. a2.eat();
16. a3.eat();
17. }
18. }
Object-Oriented Programming
ASSESSMENT TASK
Identify the output of the following programs:
class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
} Output of the Program:
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}
Object-Oriented Programming
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
} Output of the Program:
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
Start your lesson here.
Java instanceof
The java instanceof operator is used to test whether the object is an instance of the
specified type (class or subclass or interface). The instanceof in java is also known as
type comparison operator because it compares the instance with type. It returns either true or
false. If we apply the instanceof operator with any variable that has null value, it returns false.
Let's see the simple example of instance operator where it tests the current class.
1. class Simple1{
2. public static void main(String args[]){ Output of the Program:
3. Simple1 s=new Simple1(); true
4. System.out.println(s instanceof Simple1);//true
5. }
6. }
1. class Animal{}
2. class Dog1 extends Animal{//Dog inherits Animal
3. public static void main(String args[]){ Output of the Program:
4. Dog1 d=new Dog1();
true
5. System.out.println(d instanceof Animal);//true
6. }
7. }
Object-Oriented Programming
In this lesson, Teaching and learning is mediated through the use of technology like print,
audio, video and the internet. Students interact with their instructors and each other through
virtual classrooms, email, and web conferencing. For the online modality, the Virtual Classroom
shall be used for the purpose of delivering a lecture and allowing a synchronous discussion with
the students. For the remote modality, Self-directed (SeDI) a learning management system shall
be used to upload the module and to allow asynchronous discussion with the students. This will
also be used as platform for the submission of the requirements.
Object-Oriented Programming
ASSESSMENT TASK
Identify the output of the following programs:
class Animal { }
class Dog3 extends Animal {
static void method(Animal a) {
if(a instanceof Dog3){ Output of the Program:
Dog3 d=(Dog3)a;
System.out.println("ok downcasting performed");
}
}
public static void main (String [] args) {
Animal a=new Dog3();
Dog3.method(a);
}
}
Object-Oriented Programming
References:
1. https://www.programiz.com/java-programming
2. https://www.javatpoint.com
3. https://beginnersbook.com/2017
4. https://www.w3schools.com/java
5. https://www.geeksforgeeks.org
6. https://www.tutorialspoint.com/java