[go: up one dir, main page]

0% found this document useful (0 votes)
24 views28 pages

Module 8

Uploaded by

Rica Guiuo
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)
24 views28 pages

Module 8

Uploaded by

Rica Guiuo
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/ 28

Object-Oriented Programming

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:

At the end of the chapter the students should be able:


 Differentiate method overloading and method overriding
 Apply method overloading and method overriding
 Understand and apply the super keyword
 Understand and apply the final keyword
 Understand and apply runtime polymorphism
 Understand and apply the intanceof operator

Start your lesson here.

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.

So, method overloading is to be performed to figure out the program


quickly.

Advantage of method overloading

 Method overloading increases the readability of the program.

Different ways to overload the method

 By changing number of arguments


Object-Oriented Programming

 By changing the data type

NOTE: Method Overloading is not possible by changing the return type of the
method only.

1. By changing number of arguments

In this example, two methods were created, first add() method


performs addition of two numbers and second add method performs
addition of three numbers.

In this example, we are creating static methods so that we don't


need to create instance for calling methods.

1. 1. class Adder{
2. 2. static int add(int a,int b){return a+b;}
3. 3. static int add(int a,int b,int c) Output of the
{return a+b+c;} Program:
4. 4. }
22
5. 5. class TestOverloading1{
33
6. 6. public static void main(String[] args){
7. 7. System.out.println(Adder.add(11,11));
8. 8. System.out.println(Adder.add(11,11,11));
9. 9. }}

2. By changing data type of arguments

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
4. 4. } Program:
5. 5. class TestOverloading2{
22
6. 6. public static void main(String[] args){ 24.9
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.
Object-Oriented Programming

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{
6. 6. public static void main(String[] args){
7. 7. System.out.println(Adder.add(11,11));//ambiguity
8. 8. }}

Output of the Program:


Compile Time Error: method add(int,int) is already
defined in class Adder

QUESTION: Can we overload java main() method?


ANSWER: Yes, by method overloading. You can have any number of main
methods in a class by method overloading. But JVM calls main() method which
receives string array as arguments only.

Let's see the example:

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");}

Output of the
Program:

main with String[]

Method Overloading and Type Promotion

One type is promoted to another implicitly if no matching datatype is


found. Let's understand the concept by the figure given below:
Object-Oriented Programming

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

Output of the
Program:
40
60
Method Overloading with Type Promotion if matching found

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();
Object-Oriented Programming

Output of the
Program:

int arg method


invoked

ASSESSMENT TASK

Identify the output of the following programs:

public class MyClass {


static int plusMethodInt(int x, int y) {
return x + y;
}
static double plusMethodDouble(double x, double y) Output of the
{ Program:
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
Object-Oriented Programming

package com.journaldev.methodoverloading;
public class TypePromotionExample {
void add(float dataOne, float dataTwo) {
System.out.println(dataOne + dataTwo
+ " type float"); Output of the
} Program:
void add(long dataOne, long dataTwo) {
System.out.println(dataOne + dataTwo
+ " type long");
}
public static void main(String[] args) {
TypePromotionExample tpe = new
TypePromotionExample();
tpe.add(5, 5);
}
}

Start your lesson here.

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.

Usage of Java Method Overriding

 Method overriding is used to provide the specific implementation of a


method which is already provided by its superclass.
Object-Oriented Programming

 Method overriding is used for runtime polymorphism

Rules for Java 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).

Understanding the problem without method overriding

Let's understand the problem that we may face in the program if we


don't use method overriding.

1.
//Java Program to demonstrate why method overriding is
needed
2.
//Here, we are calling the method of parent class with chi
ld
3. //class object.
4. //Creating a parent class Output of the
Program:
5. class Vehicle{
6. void run(){System.out.println("Vehicle is running");} Vehicle is running
7. }
8. //Creating a child class
9. class Bike extends Vehicle{
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. }
Problem is that it is needed to provide a specific implementation of
16. }method in subclass that is why method overriding is used.
run()

Example of method overriding

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 Overri
ding
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
Object-Oriented Programming

Output of the
Program:

Bike is running
safely

A real example of Java Method Overriding

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.

Java method overriding is mostly used in Runtime Polymorphism.

1.
//Java Program to demonstrate the real scenario of Java Method Over
riding
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{ Output of the
9. int getRateOfInterest(){return 8;} Program:
10. } SBI Rate of Interest: 8
ICICI Rate of Interest: 7
11. class ICICI extends Bank{
AXIS Rate of Interest: 9
12. int getRateOfInterest(){return 7;}
13. }
14. class AXIS extends Bank{
Object-Oriented Programming

QUESTION 1: Can we override static method?


ANSWER: No, a static method cannot be overridden. It can be proved by runtime
polymorphism.

QUESTION 2: Why can we not override static method?


ANSWER: It is because the static method is bound with class whereas instance
method is bound with an object. Static belongs to the class area, and an instance
belongs to the heap area.

QUESTION 3: Can we override java main method?


ANSWER: No, because the main is a static method.

ASSESSMENT TASK

Identify the output of the following programs:

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();
}
}
Object-Oriented Programming

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();
}
}

Start your lesson here.

The Super Keyword

The super keyword in Java is a reference variable which is used to refer


immediate parent class object.

Whenever you create the instance of subclass, an instance of parent


class is created implicitly which is referred by super reference variable.

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

1. Super can be 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.
Object-Oriented Programming

Example:

1. class Animal{
2. String color="white";
3. }
4. class Dog extends Animal{
5. String color="black";
6. void printColor(){ Output of the
Program:
7. System.out.println(color);//prints color of Dog class
8. black
System.out.println(super.color);//prints color of Animalclas white
s
9. }
10. }
11. class TestSuper1{
12. public static void main(String args[]){
13. Dog d=new Dog();
14. d.printColor();
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.

2. Super can be used to invoke immediate 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.

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
7. void work(){ Program:

8. super.eat(); eating…
9. bark(); barking…
10. }
11. }
12. class TestSuper2{
13. public static void main(String args[]){
14. Dog d=new Dog();
15. d.work();
16. }}
Object-Oriented Programming

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.

3. Super() can be used to invoke immediate parent class


constructor.

Example 1:
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
Output of the
5. Dog(){ Program:
6. super();
7. System.out.println("dog is created"); animal is created
dog is created
8. }
9. }
10. class TestSuper3{
11. public static void main(String args[]){
12. Dog d=new Dog();
13. }}
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. }
8. } 1 ankit 45000
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;
Object-Oriented Programming

ASSESSMENT TASK

Identify the output of the following programs:

class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{ Output of the
int num = 110; Program:
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() {
Object-Oriented Programming

Output of the
Program:

Start your lesson here.

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.
Object-Oriented Programming

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
5. } Program:
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.

Example:

1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4. class Honda extends Bike{
5. void run()
Output of the
{System.out.println("running safely with 100kmph"); Program:
6. }
Compile Time Error
7. public static void main(String args[]){
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:
Object-Oriented Programming

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");} Output of the
4. public static void main(String args[]){ Program:
5. Honda1 honda= new Honda1();
Compile Time Error
6. honda.run();
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. } Output of the
4. class Honda2 extends Bike{ Program:
5. public static void main(String args[]){ running…
6. new Honda2().run();
7. }
8. }
Object-Oriented Programming

ASSESSMENT TASK

Identify the output of the following programs:

class StudentData{
final int ROLL_NO;
StudentData(int rnum){
Output of the
ROLL_NO=rnum;
Program:
}
void myMethod(){
System.out.println("Roll no is:"+ROLL_NO);
}
public static void main(String args[]){
StudentData obj=new StudentData(1234);
obj.myMethod();
}
}

class XYZ{
final void demo(){
System.out.println("XYZ Class Method");
} Output of the
} Program:

class ABC extends XYZ{


public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
Object-Oriented Programming

Start your lesson here.

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

Runtime Polymorphism in Java


Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
In this process, an overridden method is called through the reference
variable of a superclass. The determination of the method to be called is
based on the object being referred to by the reference variable.
Let's first understand the upcasting before Runtime Polymorphism.

Upcasting
If the reference variable of Parent class refers to the object of Child
class, it is known as upcasting. For example:
Object-Oriented Programming

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{}

Here, the relationship of B class would be:


B IS-A A
B IS-A I
B IS-A Object
Since Object is the root class of all classes in Java, so we can write B IS-A
Object.

Example of Java Runtime Polymorphism


In this example, we are creating two classes Bike and Splendor.
Splendor class extends Bike class and overrides its run() method. We are
calling the run method by the reference variable of Parent class. Since it
refers to the subclass object and subclass method overrides the Parent class
method, the subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is
known as runtime polymorphism.

1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{
5. void run()
{System.out.println("running safely with 60km");}
6. public static void main(String args[]){
Object-Oriented Programming

Output of the
Program:

running safely with 60


km

Java Runtime Polymorphism Example: Bank


Consider a scenario where Bank is a class that provides a method to
get the rate of interest. However, the rate of interest may differ according to
banks. For example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and
9.7% rate of interest.

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{
5. float getRateOfInterest(){return 8.4f;}
6. }
7. class ICICI extends Bank{
8. float getRateOfInterest(){return 7.3f;}
9. }
10. class AXIS extends Bank{
Output of the
11. float getRateOfInterest(){return 9.7f;} Program:
12. }
SBI Rate of Interest: 8.4
13. class TestPolymorphism{ ICICI Rate of Interest: 7.3
14. public static void main(String args[]){ AXIS Rate of Interest: 9.7
15. Bank b;
16. b=new SBI();
17.
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
Object-Oriented Programming

Java Runtime Polymorphism Example: Shape

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...");}
Output of the
9. } 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();
20. s=new Triangle();
21. s.draw();
22. }
23. }
Object-Oriented Programming

Java Runtime Polymorphism Example: Animal

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. }
10. class Lion extends Animal{ eating bread...
11. void eat(){System.out.println("eating meat...");} eating rat...
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();
21. a.eat();
22. }}

Java Runtime Polymorphism with Data Member


A method is overridden, not the data members, so runtime
polymorphism can't be achieved by data members. In the example given
below, both the classes have a data member speedlimit. We are accessing
the data member by the reference variable of Parent class which refers to the
Object-Oriented Programming

subclass object. Since we are accessing the data member which is not
overridden, hence it will access the data member of the Parent class always.
Rule: Runtime polymorphism can't be achieved by data members.

1. class Bike{
2. int speedlimit=90;
3. }
Output of the
4. class Honda3 extends Bike{ Program:
5. int speedlimit=150;
6. public static void main(String args[]){ 90

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
7. class BabyDog extends Dog{
Program:
8. void eat(){System.out.println("drinking milk");}
9. public static void main(String args[]){ eating
eating fruits
10. Animal a1,a2,a3;
drinking Milk
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);
}
}
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");
}
}
Object-Oriented Programming

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.

Simple example of java instanceof

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();
4. System.out.println(s instanceof Simple1);//true true
5. }
6. }

1. class Animal{}
2. class Dog1 extends Animal{//Dog inherits Animal
3. public static void main(String args[]){ Output of the
4. Dog1 d=new Dog1(); Program:

5. System.out.println(d instanceof Animal);//true true


6. }
7. }
Object-Oriented Programming

Recommended Learning Materials and Resources


To supplement the lesson in this module, you may visit the following
links:
 https://www.programiz.com/java-programming
 https://www.javatpoint.com
 https://beginnersbook.com/2017
 https://www.w3schools.com/java

Flexible Teaching Learning Modality (FTLM) adapted

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:

public class MyClass {


public static void main(String[] args) { Output of the
MyClass myObj = new MyClass(); Program:
System.out.println(myObj instanceof MyClass); //
returns true
}
}

class Dog2{ Output of the


public static void main(String args[]){ Program:
Dog2 d=null;
System.out.println(d instanceof Dog2);//false
}
}

class Animal { }
class Dog3 extends Animal {
static void method(Animal a) {
if(a instanceof Dog3){ Output of the
Dog3 d=(Dog3)a; Program:
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

You might also like