Search This Blog

27 June, 2011

Dynamic polymorphism, extending interfaces and adapter classes


– An object reference may be declared to
be of interface type.
– This reference may be assigned at run
time to any one of various classes which
implements the interface.
– The implemented methods of that class
will be called.


• Inheritance of interfaces:
– One interface can extend another
interface.


interface mine extends yours
{
//new methods
]


• A class which now implements interface mine
must implement all the methods in yours and
mine.


• However, there are Adapter classes which enable
one to implement only part of an interface.



Program :-
interface Area
{
final float pi = 3.14f;
float area (float diameter);
}
class Circle implements Area
{
public float area (float diameter)
{
return pi*diameter;
}
}
class Square implements Area
{
public float area (float diameter)
{
return diameter*diameter;
}
}

public class Interf
{
public static void main (String args[])
{
Area a;
a = new Circle();
System.out.println (“Area of circle = ” +
a.area (2.0f));
a = new Square();
System.out.println (“Area of square= ”+
a.area (2.0f));
}
}
• Output:
Area of circle = 6.38
Area of square = 4
---------------------------------------------
Inner classes and multiple inheritance in Java
---------------------------------------------

No comments:

Post a Comment

Meetme@