java polymorphism n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java Polymorphism PowerPoint Presentation
Download Presentation
Java Polymorphism

Loading in 2 Seconds...

play fullscreen
1 / 38

Java Polymorphism - PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on

Java Polymorphism. What we have learned. Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?. What we have learned. Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java Polymorphism' - bailey


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what we have learned
What we have learned
  • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP.
  • What are the other two?
what we have learned1
What we have learned
  • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP.
  • What are the other two?
    • Encapsulation
    • Inheritance
what we have learned2
What we have learned
  • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP.
  • Why do we need polymorphism?
    • To achieve software reuse
    • Facilitate software maintenance and upgrading
what we have learned3
What we have learned
  • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP.
  • What are the possible forms of polymorphism in C++?
    • virtual functions (related to type auto-conversion)
    • templates
recall
Recall…
  • What does polymorphism mean?
recall1
Recall…
  • What does polymorphism mean?
    • It refers to the ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding.
    • What does late binding mean?
recall2
Recall…
  • What does polymorphism mean?
    • It refers to the ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding.
    • What does late binding mean?
      • determining the instance function based on the type of the calling object during run time!
recall3
Recall…
  • Inheritance allows us to define a base class and to define software for the base class. That software can then be used not only for objects of the base class but also for objects of any class derived from the base class.
  • Polymorphism allows us to make changes in the method definition for the derived classes and to have those changes apply to the software written in the base class.
recall4
Recall…
  • Inheritance allows us to define a base class and to define software for the base class. That software can then be used not only for objects of the base class but also for objects of any class derived from the base class.
  • Polymorphism allows us to make changes in the method definition for the derived classes and to have those changes apply to the software written in the base class.
  • This is because Java (also C++) permits an object of a subclass type to be treated as an object of any superclass type! This is known as upcasting! It is done automatically!
how did we do that in c
How did we do that in C++?

Do you still recall the Figure example from C++?

how did we do that in c1
How did we do that in C++?

Do you still recall the Figure example from C++?

Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram.

how did we do that in c2
How did we do that in C++?

Do you still recall the Figure example from C++?

Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram.

There is an external function (or a user) that wants to use the draw() function to draw these different shapes.

void fun(Figure *fpt, float ncx, float ncy)

{

fpt->set_center(ncx, ncy);

fpt->draw();

}

how did we do that in c3
How did we do that in C++?

Do you still recall the Figure example from C++?

Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram.

Our goal

To facilitate the future maintenance, we want to keep the interface of this function unchanged despite the new inclusion of more derived classes.

void fun(Figure *fpt, float ncx, float ncy)

{

fpt->set_center(ncx, ncy);

fpt->draw();

}

how did we do that in c4
How did we do that in C++?

class Circle : public Figure

{

public:

Circle(float cx=0, float cy=0, float r=0)

: Figure(cx, cy)),radius(r) {}

virtual void draw()

{new implementation for Circle}

……

private:

float radius;

};

The solution in C++?

virtual functions!

class Figure

{

public:

Figure (float cx=0, float cy=0)

{}

virtualvoid draw(){…}

void Center(){…}

private:

float center_x, center_y;

};

class Rectangle: public Figure

{

public:

Rectangle(float cx=0, float cy=0,

float w=0, float h=0)

: Figure(cx, cy)

{width=w; height=h;}

virtual void draw()

{new implementation for Rectangle}

……

private:

float radius;

};

how did we do that in c5
How did we do that in C++?

class Circle : public Figure

{

public:

Circle(float cx=0, float cy=0, float r=0)

: Figure(cx, cy)),radius(r) {}

virtual void draw()

{new implementation for Circle}

……

private:

float radius;

};

The solution in C++?

virtual functions!

class Figure

{

public:

Figure (float cx=0, float cy=0)

{}

virtualvoid draw(){…}

void Center(){…}

private:

float center_x, center_y;

};

class Rectangle: public Figure

{

public:

Rectangle(float cx=0, float cy=0,

float w=0, float h=0)

: Figure(cx, cy)

{width=w; height=h;}

virtual void draw()

{new implementation for Rectangle}

……

private:

float radius;

};

void fun(Figure *fpt, float ncx, float ncy)

{

fpt->set_center(ncx, ncy);

fpt->draw();

}

Late binding!

what about java
What about Java?

Do we have the same mechanism in Java?

what about java1
What about Java?

Do we have the same mechanism in Java?

Yes and No

what about java2
What about Java?

Do we have the same mechanism in Java?

Yes and No

“Yes” means that Java also supports polymorphism

“No” says that it is not achieved using virtual function!

what about java3
What about Java?

Do we have the same mechanism in Java?

Yes and No

“Yes” means that Java also supports polymorphism

“No” says that it is not achieved using virtual function!

Particularly, it is achieved automatically without special treatment!

what about java4
What about Java?

Do we have the same mechanism in Java?

Yes and No

“Yes” means that Java also supports polymorphism

“No” says that it is not achieved using virtual function!

Particularly, it is achieved automatically without special treatment!

Java uses late binding for all the methods except……

let us put the except… part aside at this moment

recall example
Recall Example
  • public class Base
  • {
    • int x, y = 0;
    • public Base(intx_val, inty_val)
    • {
    • x = x_val; y = y_val;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0, 0);
    • }
    • ……
  • }
  • public class Try
  • {
    • public static void main(String[] args)
    • {
    • Base b_obj = new Base();
    • Derived d_obj = new Derived();
    • print(b_obj);
    • print(d_obj);
    • }
    • public static void print(Base obj)
    • {
    • System.out.println(“x=“+obj.x);
    • System.out.println(“y=“+obj.y);
    • }
  • }
  • public class Derived extends Base
  • {
    • private double x, y = 0;
    • public Derived()
    • {
    • this (0.0, 1.0);
    • }
    • public Derived(double x_val, double y_val)
    • {
    • super ((int)x_val, (int)y_val);
    • x = x_val; y = y_val;
    • System.out.println(“Derived class.”);
    • }
    • ……
  • }

Base class.

Base class.

Derived class.

x=0

y=0

x=0

y=1

recall example1
Recall Example
  • public class Base
  • {
    • int x, y = 0;
    • public Base(intx_val, inty_val)
    • {
    • x = x_val; y = y_val;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0, 0);
    • }
    • ……
  • }
  • public class Try
  • {
    • public static void main(String[] args)
    • {
    • Base b_obj = new Base();
    • Derived d_obj = new Derived();
    • print(b_obj);
    • print(d_obj);
    • }
    • public static void print(Base obj)
    • {
    • System.out.println(“x=“+obj.x);
    • System.out.println(“y=“+obj.y);
    • }
  • }
  • public class Derived extends Base
  • {
    • private double x, y = 0;
    • public Derived()
    • {
    • this (0.0, 1.0);
    • }
    • public Derived(double x_val, double y_val)
    • {
    • super ((int)x_val, (int)y_val);
    • x = x_val; y = y_val;
    • System.out.println(“Derived class.”);
    • }
    • ……
  • }

Base class.

Base class.

Derived class.

x=0

y=0

x=0

y=1

How to achieve output?

……

x=0.0

y=1.0

example
Example
  • public class Base
  • {
    • int x, y = 0;
    • public Base(intx_val, inty_val)
    • {
    • x = x_val; y = y_val;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0, 0);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class Derived extends Base
  • {
    • private double x, y = 0;
    • public Derived()
    • {
    • this (0.0, 1.0);
    • }
    • public Derived(double x_val, double y_val)
    • {
    • super ((int)x_val, (int)y_val);
    • x = x_val; y = y_val;
    • System.out.println(“Derived class.”);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class TryDemo
  • {
    • public static void main(String[] args)
    • {
    • Base b_obj = new Base();
    • Derived d_obj = new Derived();
    • b_obj.print();
    • d_obj.print();
    • }
  • }

We know this is going to work!

example1
Example
  • public class Base
  • {
    • int x, y = 0;
    • public Base(intx_val, inty_val)
    • {
    • x = x_val; y = y_val;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0, 0);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class Derived extends Base
  • {
    • private double x, y = 0;
    • public Derived()
    • {
    • this (0.0, 1.0);
    • }
    • public Derived(double x_val, double y_val)
    • {
    • super ((int)x_val, (int)y_val);
    • x = x_val; y = y_val;
    • System.out.println(“Derived class.”);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class TryDemo
  • {
    • public static void main(String[] args)
    • {
    • Base b_obj = new Base();
    • Derived d_obj = new Derived();
    • b_obj.print();
    • d_obj.print();
    • }
  • }

Can we achieve that using polymorphism?

example2
Example
  • public class Base
  • {
    • int x, y = 0;
    • public Base(intx_val, inty_val)
    • {
    • x = x_val; y = y_val;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0, 0);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class Derived extends Base
  • {
    • private double x, y = 0;
    • public Derived()
    • {
    • this (0.0, 1.0);
    • }
    • public Derived(double x_val, double y_val)
    • {
    • super ((int)x_val, (int)y_val);
    • x = x_val; y = y_val;
    • System.out.println(“Derived class.”);
    • }
    • public void print()
    • {
    • System.out.println(“x=“+x);
    • System.out.println(“y=“+y);
    • }
  • }
  • public class TryDemo
  • {
    • public static void main(String[] args)
    • {
    • Base b_obj = new Base();
    • Derived d_obj = new Derived();
    • print(b_obj);
    • print(d_obj);
    • }
    • public static void print(Base obj)
    • { obj.print(); }
  • }

Yes, see the left.

let us go back to the figure example
Let us go back to the Figure example

public class Figure

{

public Figure (double cx, double cy)

{ center_x=cx; center_y=cy;}

public void draw()

{System.out.println(“A figure.”); }

public void area()

{System.out.println(“No define.”); }

public void reset_center(double x, double y)

{ center_x=x; center_y=y;}

private double center_x=0, center_y=0;

};

public class TestFigure

{

public TestFigure()

{ }

public static void main(String[] args)

{ Figure f1 = new Square (0, 0, 1);

Figure f2 = new Figure (3, 4);

Square s1 = new Square (2.3, 3.5, 3);

Center(f1, 1, 1);

getArea(f1);

Center(f2,3,4);

getArea (f2);

Center(f3,-5, -4);

getArea (f3);

}

public void static Center(Figure obj, float cx, float cy)

{ obj.reset_center(cx,cy);

obj.draw();

}

public void static getArea(Figure obj)

{ obj.area();

}

}

public class Square extends Figure

{

public Square(double cx, double cy, double size)

{ super(); this.size=size;}

public void draw()

{ System.out.println(“A Square.”); }

public double area()

{ return size*size; }

private double size=0;

};

What are the issues of this code?

let us go back to the figure example1
Let us go back to the Figure example

public class Figure

{

public Figure (double cx, double cy)

{ center_x=cx; center_y=cy;}

public void draw()

{System.out.println(“A figure.”); }

public void area()

{System.out.println(“No define.”); }

public void reset_center(double x, double y)

{ center_x=x; center_y=y;}

private double center_x=0, center_y=0;

};

public class TestFigure

{

public TestFigure()

{ }

public static void main(String[] args)

{ Figure f1 = new Square (0, 0, 1);

Figure f2 = new Figure (3, 4);

Square s1 = new Square (2.3, 3.5, 3);

Center(f1, 1, 1);

getArea(f1);

Center(f2,3,4);

getArea (f2);

Center(f3,-5, -4);

getArea (f3);

}

public void static Center(Figure obj, float cx, float cy)

{ obj.reset_center(cx,cy);

obj.draw();

}

public void static getArea(Figure obj)

{ obj.area();

}

}

public class Square extends Figure

{

public Square(double cx, double cy, double size)

{ super(); this.size=size;}

public void draw()

{ System.out.println(“A Square.”); }

public double area()

{ return size*size; }

private double size=0;

};

What are the issues of this code?

let us go back to the figure example2
Let us go back to the Figure example

public class Figure

{

public Figure (double cx, double cy)

{ center_x=cx; center_y=cy;}

public Figure()

{ this (0,0); }

public void draw()

{System.out.println(“A figure.”); }

public void area()

{System.out.println(“No define.”); }

public void reset_center(double x, double y)

{ center_x=x; center_y=y;}

private double center_x=0, center_y=0;

};

public class TestFigure

{

public TestFigure()

{ }

public static void main(String[] args)

{ Figure f1 = new Square (0, 0, 1);

Figure f2 = new Figure (3, 4);

Square f3 = new Square (2.3, 3.5, 3);

Center(f1, 1, 1);

getArea(f1);

Center(f2,3,4);

getArea (f2);

Center(f3,-5, -4);

getArea (f3);

}

public static void Center(Figure obj, float cx, float cy)

{ obj.reset_center(cx,cy);

obj.draw();

}

public static void getArea(Figure obj)

{ obj.area();

}

}

public class Square extends Figure

{

public Square(double cx, double cy, double size)

{ super(cx, cy); this.size=size;}

public void draw()

{ System.out.println(“A Square.”); }

public void area()

{System.out.println("The area of the square is "+ size*size);}

private double size=0;

};

So, what will be the output?

polymorphism in java
Polymorphism in Java

Java uses late binding for all the methods except……

Now let us look at the except part.

What are the methods that the late binding is not applied?

polymorphism in java1
Polymorphism in Java

Java uses late binding for all the methods except……

Now let us look at the except part.

What are the methods that the late binding is not applied?

  • They are two types of these methods
    • static methods
    • final methods

In both cases, early binding is applied.

abstract class
Abstract Class

Similar to C++, abstract classes are also allowed in Java.

So what does an abstract class?

abstract class1
Abstract Class

Similar to C++, abstract classes are also allowed in Java.

So what does an abstract class?

It allows us to postpone the definition of certain methods in a base class.

For example, in the previous Figure example, both draw() and area() functions in the Figure class need not have a definition as they only make sense when a more specific shape is provided, e.g. a circle or a square.

abstract class2
Abstract Class

To postpone the definition of a method, it is specified as an abstract method.

public abstract class Figure

{

public Figure (double cx, double cy)

{ center_x=cx; center_y=cy;}

public Figure()

{ this (0,0); }

public abstract void draw();

public abstract void area();

public void reset_center(double x, double y)

{ center_x=x; center_y=y;}

private double center_x=0, center_y=0;

};

Abstract methods define the interface of the base class and all its derived classes, because…

A class that has at least one abstract method is called an abstract class.

An abstract class must have the “abstract” modifier in its class heading

abstract class3
Abstract Class
    • An abstract class can have any number of abstract and/or fully defined methods
    • If a derived class of an abstract class adds to or does not define all of the abstract methods, then it is abstract also, and must add abstractto its modifier
  • A class that has no abstract methods is called a concrete class
example3
Example
  • public abstract class Base
  • {
  • protected int type = 0;
    • public Base(inttype)
    • { type = type;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0); }
    • public abstract intgetType();
    • public String toString()
    • {
    • return Integer.toString(getType());
    • }
  • }
  • public class Derived2 extends Derived
  • {
  • private intderived_type = 2;
    • public Derived2 (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class 2.”);
    • }
    • public Derived2()
    • { this(2);}
    • public intgetType()
    • { System.out.print(“Derived class 2 type ”);
    • return type;
    • }
  • }

public class TestDemo

{

public static void main(String[] args)

{

Base obj1 = new Derived(0,1);

Base obj2 = new Base();

Base obj3 = new Derived2(2, 3)

System.out.println(obj1);

System.out.println(obj2);

System.out.println(obj3);

}

}

  • public class Derived extends Base
  • {
  • private intderived_type = 1;
    • public Derived (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class.”);
    • }
    • public Derived()
    • { this(1); }
    • public intgetType()
    • { System.out.print(“Derived class type ”);
    • return derived_type;
    • }
  • }

What are the issues?

example4
Example
  • public abstract class Base
  • {
  • protected int type = 0;
    • public Base(inttype)
    • { type = type;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0); }
    • public abstract intgetType();
    • public String toString()
    • {
    • return Integer.toString(getType());
    • }
  • }
  • public class Derived2 extends Derived
  • {
  • private intderived_type = 2;
    • public Derived2 (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class 2.”);
    • }
    • public Derived2()
    • { this(2);}
    • public intgetType()
    • { System.out.print(“Derived class 2 type ”);
    • return type;
    • }
  • }

public class TestDemo

{

public static void main(String[] args)

{

Base obj1 = new Derived(1);

Base obj3 = new Derived2(3)

System.out.println(obj1);

System.out.println(obj3);

}

}

  • public class Derived extends Base
  • {
  • private intderived_type = 1;
    • public Derived (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class.”);
    • }
    • public Derived()
    • { this(1); }
    • public intgetType()
    • { System.out.print(“Derived class type ”);
    • return derived_type;
    • }
  • }
example5
Example
  • public abstract class Base
  • {
  • protected int type = 0;
    • public Base(inttype)
    • { type = type;
    • System.out.println(“Base class.”);
    • }
    • public Base()
    • { this (0); }
    • public abstract intgetType();
    • public String toString()
    • {
    • return Integer.toString(getType());
    • }
  • }
  • public class Derived2 extends Derived
  • {
  • private intderived_type = 2;
    • public Derived2 (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class 2.”);
    • }
    • public Derived2()
    • { this(2);}
    • public intgetType()
    • { System.out.print(“Derived class 2 type ”);
    • return type;
    • }
  • }

public class TestDemo

{

public static void main(String[] args)

{

Base obj1 = new Derived(0,1);

Base obj3 = new Derived2(2, 3)

System.out.println(obj1);

System.out.println(obj3);

}

}

  • public class Derived extends Base
  • {
  • private intderived_type = 1;
    • public Derived (inttype)
    • { super (type); derived_type=type;
    • System.out.println(“Derived class.”);
    • }
    • public Derived()
    • { this(1); }
    • public intgetType()
    • { System.out.print(“Derived class type ”);
    • return derived_type;
    • }
  • }

So, what will be the output?