In this session you will learn:
Poly- many
morphism –forms.
An entity existing in more than one form.
methods,
objects.
For more information, visit: https://www.mindsmapped.com/courses/software-development/java-developer-training-for-beginners/
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()
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();
}
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()
{
}