SlideShare a Scribd company logo
Java/J2ee Programming Training
Polymorphism
Page 2Classification: Restricted
Agenda
• Poly- many
• morphism –forms.
• An entity existing in more than one form.
• methods,
• objects.
Page 3Classification: Restricted
Polymorphism
• polymorphism
• static polymorphism
• entity exists in more than one form physically
• implemented in java using method overloading concept
• dynamic polymorphism
• type of the object is decided at run time
• implemented using
• method overriding
• polymorphic arguments, return types
• abstract classes
• interfaces.
Page 4Classification: Restricted
Polymorphism
• Method overloading
• method physically exists in more than one form.
• method with the same name but different signature list.
• signature of the method should differ atleast
• number of arguments
• data type of arguments
• order of arguments.
• which function to invoke is decided at compile time( early binding )
Page 5Classification: Restricted
Static Polymorphism
public void add( int a, int b)
Differs in Number of arguments
public void add( int a , int b)
public void add( int a, int b, int c )
Differs Datatype of arguments
public void add ( int a , int b)
public void add( int a, float b)
Differs in Order of arguments
public void add( int a, float b)
public void add( float a, int b)
Page 6Classification: Restricted
public void add( int a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( int a, int b, int c)
{
System.out.println( “sum = “+(a + b +c ) );
}
public void add( int a, float b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( float a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public static void main(String args[] ) {
int a = 10; int b = 20;
add( a, b);
}
Page 7Classification: Restricted
public void add( int a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( int a, int b, int c)
{
System.out.println( “sum = “+(a + b +c ) );
}
public static void main(String args[] ) {
int a = 10; int b = 20; int c = 30
add( a, b, c );
}
number of
argument
s varies
Page 8Classification: Restricted
public void add( int a, int b, int c)
{
System.out.println( “sum = “+(a + b +c ) );
}
public void add( int a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( int a, float b)
{
System.out.println( “sum = “+(a + b) );
}
public static void main(String args[] ) {
int a = 10; float b = 30.5;
add( a, b);
}
datatype of
arguments varies
Page 9Classification: Restricted
public void add( int a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( int a, float b)
{
System.out.println( “sum = “+(a + b) );
}
public void add( float a, int b)
{
System.out.println( “sum = “+(a + b) );
}
public static void main(String args[] ) {
float a = 10; int b = 30.5;
add( a, b);
}
order of arguments
varies
Page 10Classification: Restricted
Polymorphism
• public void add( int a , int b )
• public int add( int a , int b)
• return type is not taken into consideration
• public void add( int a , int b )
• private int add( int a , int b)
• access specifier is not taken into consideration
Page 11Classification: Restricted
DynamicPolymorphism
• Method overriding
• functions have the same signature but different logic
• methods inherited from the base class are overridden in child class.
• access specifier can be more friendly.
• supports late binding principle
public class Employee
{
private String uname,email;
public void register()
{
System.out.println(“Enter uname”);
uname = sc.next();
System.out.println(“Enter email”);
email =sc.next();
}
}
public class Employee2 extends Employee
{
private String biometrics;
public void register()
{
super.register();
System.out.println(“scan finger”);
biometrics =“finger pattern”;
}
}
Employee
String uname
String email
+ void register()
Employee2
String uname
String email
+ void register()
public class Employee
{
private String uname,email;
public void register()
{
System.out.println(“Enter uname”);
uname = sc.next();
System.out.println(“Enter email”);
email =sc.next();
}
}
public class Employee2 extends Employee
{
private String captcha;
public void register()
{
super.register();
System.out.println(“Enter Image”);
captcha=“ABc123”;
}
}
Employee
String uname
String email
+ void register()
Employee2
String uname
String email
+ void register()
Shape
String name;
int dim1;
int dim2;
Rectangle
String name;
int dim1;
int dim2;
+ void details()
+void area();
Triangle
String name;
int dim1;
int dim2;
+void details();
+void area();
+ void details()
{
S.o.p (name +” “ + dim1 + “ “ +dim2 );
}
+void area()
{
S.o.p (“area = “ + dim1 * dim2);
}
+ void details()
{
S.o.p (name +” “ + dim1 + “ “ +dim2 );
}
+void area()
{
S.o.p (“area = “ +0.5f * dim1 * dim2);
}
+ void details()
{
S.o.p (name +” “ + dim1 + “ “ +dim2 );
}
+void area()
{
S.o.p (“area = “ +0.5f * dim1 * dim2);
}
+ void details()
{
S.o.p (name +” “ + dim1 + “ “ +dim2 );
}
+ abstract void area();
Animal
String name;
int height;
int weight;
Cat
String name;
int height;
int weight;
+ void details()
+void makeNoise();
Dog
String name;
int height;
int weight;
+void details();
+void makeNoise();
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void makeNoise()
{
S.o.p (“cat meow meow“);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void makeNoise()
{
S.o.p(“Dog’s bow”);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void makeNoise()
{
S.o.p (“whattype of noise????”);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+ abstract void makeNoise();
Animal
String name;
int height;
int weight;
Cat
String name;
int height;
int weight;
+ void details()
+void area();
Dog
String name;
int height;
int weight;
+void details();
+void area();
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void makeNoise()
{
S.o.p (“cat meow meow“);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void makeNoise()
{
S.o.p(“Dog’s bow”);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+void area()
{
S.o.p (“whattype of noise????”);
}
+ void details()
{
S.o.p (name +” “ + height+ “ “ +weight);
}
+ abstract void makeNoise();
Animal is an abstract
class
Cat is
concrete
Dog is
concrete
Concrete
method
Concrete
method
Page 17Classification: Restricted
Abstract
• Animal is an abstract class.
• We can create an object of abstract class.
• Animal animal = new Animal();
• Objects have well defined behavior.
• Object has well defined behavior, makeNoise() method in animal
class doesn’t have behavior.
• jvm doesn’t know about the object behavior and prohibits
creating the object.
Page 18Classification: Restricted
Abstract Classes
• Animal animal = new Animal();
What type of
Animal…dog..or
cat…??
do animal make same
type of Noise????
Then let us not
create the object
of Animal…make
Animal class
abstract.
Page 19Classification: Restricted
a.makeNoise()
Dog
String name;
int height;
int weight;
String xxx;
+void makeNoise()
{
S.o.p(“Dog’s
bow”);
}
Cat
String name;
int height;
int weight;
String yyy;
+void makeNoise()
{
S.o.p(“cats
meow”);
}
Animal
String name;
int height;
int weight;
+void
makeNoise()Animal a = new Dog();
a.makeNoise()
Dog
String name;
int height;
int weight;
String xxx;
+void makeNoise()
{
S.o.p(“cats meoww”);
}
Cat
String name;
int height;
int weight;
String yyy;
+void makeNoise()
{
S.o.p(“Dog’s bow”);
}
Animal
String name;
int height;
int weight;
+void makeNoise()Animal a = new Cat();
Dog
String name;
int height;
int weight;
String xxx;
+void makeNoise()
{
S.o.p(“cats meoww”);
}
Cat
String name;
int height;
int weight;
String yyy;
+void makeNoise()
{
S.o.p(“Dog’s bow”);
}
Animal
String name;
int height;
int weight;
+void makeNoise()
+void playSound( Animal a )
{
a.makeNoise();
}
Dog
String name;
int height;
int weight;
String xxx;
+void makeNoise()
{
S.o.p(“Dog meoww”);
}
Cat
String name;
int height;
int weight;
String yyy;
+void makeNoise()
{
S.o.p(“Cat meow”);
}
Animal
String name;
int height;
int weight;
+void makeNoise()
+void playSound( Animal a )
{
a.makeNoise();
}
Page 23Classification: Restricted
Interfaces
• Interfaces – prototype
• all methods declared in an interface are by default abstract
• member variables are static and final
<<Employee>>
public static final int MIN_LEAVE= 1;
public static final int MAX_LEAVE = 30;
abstract void applyLeave();
abstract void cancelLeave();
EmpFileSystem
public String filename;
public int MAX_SIZE;
+ void applyLeave()
{
//code for applyLeave
}
+ void cancelLeave()
{
}
EmpDatabaseSystem
public String dbName;
public String url
+ void applyLeave()
{
//code for applyLeave
}
+ void cancelLeave()
{
}
Page 25Classification: Restricted
Thank You

More Related Content

PDF
DevNation'15 - Using Lambda Expressions to Query a Datastore
PDF
Java Day-7
KEY
ぐだ生 Java入門第一回(equals hash code_tostring)
PDF
C# Starter L03-Utilities
PDF
C# Starter L04-Collections
PDF
Java Day-6
PPTX
Closer look at classes
PPTX
Simple class and object examples in java
DevNation'15 - Using Lambda Expressions to Query a Datastore
Java Day-7
ぐだ生 Java入門第一回(equals hash code_tostring)
C# Starter L03-Utilities
C# Starter L04-Collections
Java Day-6
Closer look at classes
Simple class and object examples in java

What's hot (20)

PDF
C# Starter L02-Classes and Objects
KEY
About java
PPT
JAVA OOP
ODP
2.1 Recap From Day One
PPTX
Front end fundamentals session 1: javascript core
PPTX
Object Oriented JavaScript
PDF
Les nouveautés de C# 6
PDF
The Ring programming language version 1.9 book - Part 84 of 210
PDF
ハイブリッド言語Scalaを使う
PDF
The Ring programming language version 1.8 book - Part 7 of 202
PDF
Object calisthenics
PDF
The Ring programming language version 1.5.3 book - Part 31 of 184
PDF
C h 04 oop_inheritance
PDF
Scala vs java 8
PDF
Java OOP Programming language (Part 3) - Class and Object
PDF
Object Oriented Solved Practice Programs C++ Exams
PPT
String and string manipulation
PDF
Java Puzzle
PPTX
20.3 Java encapsulation
C# Starter L02-Classes and Objects
About java
JAVA OOP
2.1 Recap From Day One
Front end fundamentals session 1: javascript core
Object Oriented JavaScript
Les nouveautés de C# 6
The Ring programming language version 1.9 book - Part 84 of 210
ハイブリッド言語Scalaを使う
The Ring programming language version 1.8 book - Part 7 of 202
Object calisthenics
The Ring programming language version 1.5.3 book - Part 31 of 184
C h 04 oop_inheritance
Scala vs java 8
Java OOP Programming language (Part 3) - Class and Object
Object Oriented Solved Practice Programs C++ Exams
String and string manipulation
Java Puzzle
20.3 Java encapsulation
Ad

Similar to Java Polymorphism Part 2 (20)

DOCX
Assignment 7
PPT
Java Tutorials
PPT
java training faridabad
PPTX
L02 Software Design
PPTX
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
PPSX
OOP with Java - Continued
PPTX
L03 Software Design
DOCX
JAVA Notes - All major concepts covered with examples
PPTX
Session 08 - OOP with Java - continued
PPTX
Object oriented concepts
PDF
OOPs Concepts - Android Programming
PPT
Object Oriented Programming with Java
PPTX
introduction to object oriented programming language java
PPTX
Object Oriented Programming Concepts
PDF
Java OO Revisited
PPT
Inheritance & Polymorphism - 2
PPTX
Java oops features
PPT
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
PPT
8 polymorphism
Assignment 7
Java Tutorials
java training faridabad
L02 Software Design
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
OOP with Java - Continued
L03 Software Design
JAVA Notes - All major concepts covered with examples
Session 08 - OOP with Java - continued
Object oriented concepts
OOPs Concepts - Android Programming
Object Oriented Programming with Java
introduction to object oriented programming language java
Object Oriented Programming Concepts
Java OO Revisited
Inheritance & Polymorphism - 2
Java oops features
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
8 polymorphism
Ad

More from AathikaJava (17)

PPTX
Java While Loop
PPTX
Java Webservices
PPTX
Java Type Casting
PPTX
Spring Web MVC
PPTX
Java Session
PPTX
Java Servlet Lifecycle
PPTX
Java Rest
PPTX
Java Request Dispatcher
PPTX
Java MVC
PPTX
Java Polymorphism
PPTX
Java Spring
PPTX
Mapping Classes with Relational Databases
PPTX
Introduction to Java
PPTX
Java Encapsulation and Inheritance
PPT
Hibernate basics
PPTX
Java Filters
PPTX
Encapsulation
Java While Loop
Java Webservices
Java Type Casting
Spring Web MVC
Java Session
Java Servlet Lifecycle
Java Rest
Java Request Dispatcher
Java MVC
Java Polymorphism
Java Spring
Mapping Classes with Relational Databases
Introduction to Java
Java Encapsulation and Inheritance
Hibernate basics
Java Filters
Encapsulation

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
A comparative analysis of optical character recognition models for extracting...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Programs and apps: productivity, graphics, security and other tools
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Assigned Numbers - 2025 - Bluetooth® Document
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Java Polymorphism Part 2

  • 2. Page 2Classification: Restricted Agenda • Poly- many • morphism –forms. • An entity existing in more than one form. • methods, • objects.
  • 3. Page 3Classification: Restricted Polymorphism • polymorphism • static polymorphism • entity exists in more than one form physically • implemented in java using method overloading concept • dynamic polymorphism • type of the object is decided at run time • implemented using • method overriding • polymorphic arguments, return types • abstract classes • interfaces.
  • 4. Page 4Classification: Restricted Polymorphism • Method overloading • method physically exists in more than one form. • method with the same name but different signature list. • signature of the method should differ atleast • number of arguments • data type of arguments • order of arguments. • which function to invoke is decided at compile time( early binding )
  • 5. Page 5Classification: Restricted Static Polymorphism public void add( int a, int b) Differs in Number of arguments public void add( int a , int b) public void add( int a, int b, int c ) Differs Datatype of arguments public void add ( int a , int b) public void add( int a, float b) Differs in Order of arguments public void add( int a, float b) public void add( float a, int b)
  • 6. Page 6Classification: Restricted public void add( int a, int b) { System.out.println( “sum = “+(a + b) ); } public void add( int a, int b, int c) { System.out.println( “sum = “+(a + b +c ) ); } public void add( int a, float b) { System.out.println( “sum = “+(a + b) ); } public void add( float a, int b) { System.out.println( “sum = “+(a + b) ); } public static void main(String args[] ) { int a = 10; int b = 20; add( a, b); }
  • 7. Page 7Classification: Restricted public void add( int a, int b) { System.out.println( “sum = “+(a + b) ); } public void add( int a, int b, int c) { System.out.println( “sum = “+(a + b +c ) ); } public static void main(String args[] ) { int a = 10; int b = 20; int c = 30 add( a, b, c ); } number of argument s varies
  • 8. Page 8Classification: Restricted public void add( int a, int b, int c) { System.out.println( “sum = “+(a + b +c ) ); } public void add( int a, int b) { System.out.println( “sum = “+(a + b) ); } public void add( int a, float b) { System.out.println( “sum = “+(a + b) ); } public static void main(String args[] ) { int a = 10; float b = 30.5; add( a, b); } datatype of arguments varies
  • 9. Page 9Classification: Restricted public void add( int a, int b) { System.out.println( “sum = “+(a + b) ); } public void add( int a, float b) { System.out.println( “sum = “+(a + b) ); } public void add( float a, int b) { System.out.println( “sum = “+(a + b) ); } public static void main(String args[] ) { float a = 10; int b = 30.5; add( a, b); } order of arguments varies
  • 10. Page 10Classification: Restricted Polymorphism • public void add( int a , int b ) • public int add( int a , int b) • return type is not taken into consideration • public void add( int a , int b ) • private int add( int a , int b) • access specifier is not taken into consideration
  • 11. Page 11Classification: Restricted DynamicPolymorphism • Method overriding • functions have the same signature but different logic • methods inherited from the base class are overridden in child class. • access specifier can be more friendly. • supports late binding principle
  • 12. public class Employee { private String uname,email; public void register() { System.out.println(“Enter uname”); uname = sc.next(); System.out.println(“Enter email”); email =sc.next(); } } public class Employee2 extends Employee { private String biometrics; public void register() { super.register(); System.out.println(“scan finger”); biometrics =“finger pattern”; } } Employee String uname String email + void register() Employee2 String uname String email + void register()
  • 13. public class Employee { private String uname,email; public void register() { System.out.println(“Enter uname”); uname = sc.next(); System.out.println(“Enter email”); email =sc.next(); } } public class Employee2 extends Employee { private String captcha; public void register() { super.register(); System.out.println(“Enter Image”); captcha=“ABc123”; } } Employee String uname String email + void register() Employee2 String uname String email + void register()
  • 14. Shape String name; int dim1; int dim2; Rectangle String name; int dim1; int dim2; + void details() +void area(); Triangle String name; int dim1; int dim2; +void details(); +void area(); + void details() { S.o.p (name +” “ + dim1 + “ “ +dim2 ); } +void area() { S.o.p (“area = “ + dim1 * dim2); } + void details() { S.o.p (name +” “ + dim1 + “ “ +dim2 ); } +void area() { S.o.p (“area = “ +0.5f * dim1 * dim2); } + void details() { S.o.p (name +” “ + dim1 + “ “ +dim2 ); } +void area() { S.o.p (“area = “ +0.5f * dim1 * dim2); } + void details() { S.o.p (name +” “ + dim1 + “ “ +dim2 ); } + abstract void area();
  • 15. Animal String name; int height; int weight; Cat String name; int height; int weight; + void details() +void makeNoise(); Dog String name; int height; int weight; +void details(); +void makeNoise(); + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void makeNoise() { S.o.p (“cat meow meow“); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void makeNoise() { S.o.p(“Dog’s bow”); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void makeNoise() { S.o.p (“whattype of noise????”); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } + abstract void makeNoise();
  • 16. Animal String name; int height; int weight; Cat String name; int height; int weight; + void details() +void area(); Dog String name; int height; int weight; +void details(); +void area(); + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void makeNoise() { S.o.p (“cat meow meow“); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void makeNoise() { S.o.p(“Dog’s bow”); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } +void area() { S.o.p (“whattype of noise????”); } + void details() { S.o.p (name +” “ + height+ “ “ +weight); } + abstract void makeNoise(); Animal is an abstract class Cat is concrete Dog is concrete Concrete method Concrete method
  • 17. Page 17Classification: Restricted Abstract • Animal is an abstract class. • We can create an object of abstract class. • Animal animal = new Animal(); • Objects have well defined behavior. • Object has well defined behavior, makeNoise() method in animal class doesn’t have behavior. • jvm doesn’t know about the object behavior and prohibits creating the object.
  • 18. Page 18Classification: Restricted Abstract Classes • Animal animal = new Animal(); What type of Animal…dog..or cat…?? do animal make same type of Noise???? Then let us not create the object of Animal…make Animal class abstract.
  • 19. Page 19Classification: Restricted a.makeNoise() Dog String name; int height; int weight; String xxx; +void makeNoise() { S.o.p(“Dog’s bow”); } Cat String name; int height; int weight; String yyy; +void makeNoise() { S.o.p(“cats meow”); } Animal String name; int height; int weight; +void makeNoise()Animal a = new Dog();
  • 20. a.makeNoise() Dog String name; int height; int weight; String xxx; +void makeNoise() { S.o.p(“cats meoww”); } Cat String name; int height; int weight; String yyy; +void makeNoise() { S.o.p(“Dog’s bow”); } Animal String name; int height; int weight; +void makeNoise()Animal a = new Cat();
  • 21. Dog String name; int height; int weight; String xxx; +void makeNoise() { S.o.p(“cats meoww”); } Cat String name; int height; int weight; String yyy; +void makeNoise() { S.o.p(“Dog’s bow”); } Animal String name; int height; int weight; +void makeNoise() +void playSound( Animal a ) { a.makeNoise(); }
  • 22. Dog String name; int height; int weight; String xxx; +void makeNoise() { S.o.p(“Dog meoww”); } Cat String name; int height; int weight; String yyy; +void makeNoise() { S.o.p(“Cat meow”); } Animal String name; int height; int weight; +void makeNoise() +void playSound( Animal a ) { a.makeNoise(); }
  • 23. Page 23Classification: Restricted Interfaces • Interfaces – prototype • all methods declared in an interface are by default abstract • member variables are static and final
  • 24. <<Employee>> public static final int MIN_LEAVE= 1; public static final int MAX_LEAVE = 30; abstract void applyLeave(); abstract void cancelLeave(); EmpFileSystem public String filename; public int MAX_SIZE; + void applyLeave() { //code for applyLeave } + void cancelLeave() { } EmpDatabaseSystem public String dbName; public String url + void applyLeave() { //code for applyLeave } + void cancelLeave() { }