65 Program on Abstract Class, Interface and their Relations in Java ?

Code:

interface I
{
public abstract void m1();
public abstract void m2();
}

/* Relation: Interface to Interface */
interface J extends I
{
public abstract void m3();
public abstract void m4();
}

/* Relation: Abstract Class to Interface */
abstract class L implements J
{
public void m3()
{
System.out.println(" L- M3 ");
}
}

/* Relation:  Abstract Class to  Abstract Class */
abstract class M extends L
{
public void m4()
{
System.out.println(" M- M4 ");
}

public void m2()
{
System.out.println(" M-  M2 ");
}

public void m6()
{
System.out.println(" M- M6 ");
}

public abstract void m5();
}

/* Relation:  Class to  Abstract Class */
class N extends M
{
public void m2()
{
System.out.println("N - M2 ");
}

public void m1()
{
System.out.println("N-  M1 ");
}

public void m5()
{
System.out.println(" N- M5 ");
}

}

/* Relation:  Has-A */
class Test
{
public static void main( String args[] )
{
I obj = new N();
/* In this using reference we can hold implementation or sub class memory */
obj.m1();
obj.m2();

J obj1 = new N();
obj1.m3();
obj1.m4();
obj1.m1();
obj1.m2();

M obj2 = new N();
obj2.m5();
obj2.m6();
}
}


Output:



Previous
Next Post »