java polymorphism. what we have learned polymorphism is one of the three very powerful and useful...

50
Java Polymorphism

Upload: kory-watson

Post on 18-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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?

Java Polymorphism

Page 2: 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?

Page 3: 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?– Encapsulation– Inheritance

Page 4: 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.

• Why do we need polymorphism?– To achieve software reuse– Facilitate software maintenance and upgrading

Page 5: 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 possible forms of polymorphism in C++?

Page 6: 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 possible forms of polymorphism in C++?– virtual functions (related to type auto-conversion)– templates

Page 7: 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?

Recall…

• What does polymorphism mean?

Page 8: 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?

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?

Page 9: 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?

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!

Page 10: 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?

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 classes derived from the base class.

• Polymorphism allows us to make changes in the method definitions for the derived classes and to have those changes apply to the software written in the base class.

Page 11: 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?

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 classes derived from the base class.

• Polymorphism allows us to make changes in the method definitions 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!

Page 12: 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?

How did we do that in C++?

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

Page 13: 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?

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.

Page 14: 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?

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

Page 15: 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?

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.

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

Our goal

void fun(Figure *fpt, float ncx, float ncy){ fpt->set_center(ncx, ncy); fpt->draw();}

Page 16: 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?

How did we do that in C++?The solution in C++?

virtual functions!

class Figure{public: Figure (float cx=0, float cy=0) {} virtual void draw(){…} void Center(){…}private: float center_x, center_y;};

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

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

Page 17: 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?

How did we do that in C++?The solution in C++?

virtual functions!

class Figure{public: Figure (float cx=0, float cy=0) {} virtual void draw(){…} void Center(){…}private: float center_x, center_y;};

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

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;};Late binding!

void fun(Figure *fpt, float ncx, float ncy){ fpt->set_center(ncx, ncy); fpt->draw();}

Page 18: 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 about Java?

Do we have the same mechanism in Java?

Page 19: 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 about Java?

Do we have the same mechanism in Java?

Yes and No

Page 20: 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 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!

Page 21: 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 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!

Page 22: 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 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

Page 23: 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?

Recall Examplepublic class Base{

int x, y = 0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }……

}

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=0y=0x=0y=1

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

}

Page 24: 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?

Recall Examplepublic class Base{

int x, y = 0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }……

}

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=0y=0x=0y=1

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

}

How to achieve output? ……x=0.0y=1.0

Page 25: 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?

Examplepublic class Base{

int x, y = 0;public Base(int x_val, int y_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!

Page 26: 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?

Examplepublic class Base{

int x, y = 0;public Base(int x_val, int y_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?

Page 27: 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?

Examplepublic class Base{

int x, y = 0;public Base(int x_val, int y_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);}

}

Yes, see the left.

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

}

Page 28: 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?

Let us go back to the Figure examplepublic 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 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;}

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

What are the issues of this code?

Page 29: 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?

Let us go back to the Figure examplepublic 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 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;}

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

What are the issues of this code?

Page 30: 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?

Let us go back to the Figure example

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

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

So, what will be the output?

Page 31: 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?

Polymorphism in JavaJava 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?

Page 32: 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?

Polymorphism in JavaJava 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.

Page 33: 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?

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

So what does an abstract class?

Page 34: 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?

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

Page 35: 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?

Abstract ClassTo 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

Page 36: 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?

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 abstract to its modifier

• A class that has no abstract methods is called a concrete class

Page 37: 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?

Examplepublic abstract class Base{ protected int type = 0;

public Base(int type){ type = type; System.out.println(“Base class.”);}public Base(){ this (0); }public abstract int getType();public String toString(){ return Integer.toString(getType());}

}

public class Derived extends Base{private int derived_type = 1;

public Derived (int type){ super (type); derived_type=type; System.out.println(“Derived class.”); }public Derived(){ this(1); }public int getType(){ System.out.print(“Derived class type ”); return derived_type ;}

}

public class TestDemo{ public static void main(String[] args) { Base obj1 = new Derived(1); Derived obj2 = new Base(); Base obj3 = new Derived2(3) System.out.println(obj1); System.out.println(obj2); System.out.println(obj3); }}

public class Derived2 extends Derived{private int derived_type = 2;

public Derived2 (int type){ super (type); derived_type=type; System.out.println(“Derived class 2.”); }public Derived2(){ this(2);}public int getType(){ System.out.print(“Derived class 2 type ”); return type;}

}

What are the issues?

Page 38: 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?

Example

public class TestDemo{ public static void main(String[] args) { Base obj1 = new Derived(1); Derived obj2 = new Base(); Base obj3 = new Derived2(3) System.out.println(obj1); System.out.println(obj3); }}

public abstract class Base{ protected int type = 0;

public Base(int type){ type = type; System.out.println(“Base class.”);}public Base(){ this (0); }public abstract int getType();public String toString(){ return Integer.toString(getType());}

}

public class Derived extends Base{private int derived_type = 1;

public Derived (int type){ super (type); derived_type=type; System.out.println(“Derived class.”); }public Derived(){ this(1); }public int getType(){ System.out.print(“Derived class type ”); return derived_type ;}

}

public class Derived2 extends Derived{private int derived_type = 2;

public Derived2 (int type){ super (type); derived_type=type; System.out.println(“Derived class 2.”); }public Derived2(){ this(2);}public int getType(){ System.out.print(“Derived class 2 type ”); return type;}

}

Page 39: 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?

Example

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 abstract class Base{ protected int type = 0;

public Base(int type){ this.type = type; System.out.println(“Base class.”);}public Base(){ this (0); }public abstract int getType();public String toString(){ return Integer.toString(getType());}

}

public class Derived extends Base{private int derived_type = 1;

public Derived (int type){ super (type); derived_type=type; System.out.println(“Derived class.”); }public Derived(){ this(1); }public int getType(){ System.out.print(“Derived class type ”); return derived_type ;}

}

public class Derived2 extends Derived{private int derived_type = 2;

public Derived2 (int type){ super (type); derived_type=type; System.out.println(“Derived class 2.”); }public Derived2(){ this(2);}public int getType(){ System.out.print(“Derived class 2 type ”); return type;}

}

So, what will be the output?

Page 40: 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?

Java Interfaces

Page 41: 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?

Introduction A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implement the interface.

Page 42: 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?

Introduction A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implement the interface.

public interface Interface_Name{public return_type func1(<argument list>);public void func2();public String getInfo();public boolean compareTwoObjs(Object obj1, Object obj2);……}

Page 43: 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?

Introduction A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implement the interface.

public interface Interface_Name{public return_type func1(<argument list>);public void func2();public String getInfo();public boolean compareTwoObjs(Object obj1, Object obj2);……}

Contains no instance variables!No complete method definition

public for the interface methods!

Page 44: 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?

The Figure Examplepublic interface Shape2D{ public Shape2D(double cx, double cy) { center_x=cx; center_y=cy;} public void draw(); public void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x, center_y;}

What are the issues of this interface?

Page 45: 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?

The Figure Examplepublic interface Shape2D{ public Shape2D(double cx, double cy) { center_x=cx; center_y=cy;} public void draw(); public void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x, center_y;}

It is not a class, there is no constructor!

There should not have instance variables!

Only the method heading!

Page 46: 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?

The Figure Examplepublic interface Shape2D{ public Shape2D(double cx, double cy) { center_x=cx; center_y=cy;} public void draw(); public void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x, center_y;}

It is not a class, there is no constructor!

There should not have instance variables!

Only the method heading!

interface Try {

int a=10; //public int a=10; //public static final int a=10; //final int a=10; //static int a=0;

}

But the left is allowed!All of these statements are identicalBut the initial value should be provided!

interface Try { int a; //ILLEGAL!!!}

Page 47: 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?

The Figure Examplepublic interface Shape2D{ public void draw(); public void area(); public void reset_center(double x, double y);}

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

public abstract class Figure implements Shape2D{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} 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;}

public class Circle extends Figure{ public Circle(double cx, double cy, double r) { super(cx, cy); radius=r;} public void draw() { System.out.println(“A Circle.”); } public void area() {System.out.println("The area of the circle is "+ 2*pi*radius);} private double radius=0;}

public void static Center(Shape2D obj, float cx, float cy){ obj.reset_center(cx,cy); obj.draw();}

Page 48: 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?

Another Example

public interface NodeList { public int getItem(); public Object nextNode(); }

public class ReverseList implements NodeList{ public int getItem() { } public Object nextNode() { } public void reverse (NodeList node) { } }

Page 49: 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?

Java InterfacesAgain, an interface is not a class, but can be used as type!

A Java class can implement as many interfaces as you wish.

This is similar to the multiple base classes in C++. But at the same time, it avoids the ambiguity issue of multiple inheritance in C++.

An interface definition is stored in a .java file and compiled just as a class definition is compiled.

More details are coming after the mid-term2…

A Java interface is an example of polymorphism.

Page 50: 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?

Quiz

public class TestDemo{ public static void main(String[] args) { Base obj1 = new Derived2(3); Base obj2 = new Derived(1) System.out.println(obj1); System.out.println(obj2); }}

public abstract class Base{ protected int type = 0;

public Base(int type){ this.type = type; System.out.println(“Base class.”);}public Base(){ this (0); }public abstract int getType();public String toString(){ return Integer.toString(getType());}

}

public class Derived extends Base{private int derived_type = 1;

public Derived (int type){ super (type); derived_type=type; System.out.println(“Derived class.”); }public Derived(){ this(1); }public int getType(){ System.out.print(“Derived class type ”); return derived_type ;}

}

public class Derived2 extends Derived{private int derived_type = 2;

public Derived2 (int type){ super (); derived_type=type; System.out.println(“Derived class 2.”); }public Derived2(){ this(2);}public int getType(){ System.out.print(“Derived class 2 type ”); return type;}

}

What will be the output?