r19 JNTUK JAVA LAB ; Exercise -6

Exercise-6:

 a)super keyword implementation

AIM: Write a JAVA program give example for “super” keyword

SOURCE-CODEs:

(i)Using super to call super class constructor (Without parameters)


class A

{

int l,b;

A()

{

l=10;

b=20;

}

}

class B extends A

{

int h;

B()

{

super();

h=30;

}

int volume()

{

return l*b*h;

}

}

class superdemo

{

public static void main(String args[])

{

B b1=new B();

int r=b1.volume();

System.out.println("The vol. is: "+r);

}

}


OUT-PUT:

The vol. is:6000


(ii)Using super to call super class constructor (With parameters)


class A

{

int l,b;

A(int u,int v)

{

l=u;

b=v;

}

}

class B extends A

{

int h;

B(int u,int v,int w)

{

super(u,v);

h=w;

}

int volume()

{

return l*b*h;

}

}

class superdemo

{

public static void main(String args[])

{

B b1=new B(30,20,30);

int r=b1.volume();

System.out.println("The vol. is: "+r);

}

}


OUT-PUT:

The vol. is:18000


b) Implementing interface

AIM: To write a JAVA program to implement Interface.

SOURCE-CODEs:

(i) First form of interface implementation


interface A

{

void display();

}

class B implements A

{

public void display()

{

System.out.println("B's method");

}

}

class C extends B

{

public void callme()

{

System.out.println("C's method");

}

}

class interfacedemo

{

public static void main(String args[])

{

C c1=new C();

c1.display();

c1.callme();

}

}


OUT-PUT:

B's method

C's method


(ii) Second form of interface implementation


interface D

{

void display();

}

interface E extends D

{

void show();

}

class A

{

void callme()

{

System.out.println("This is in callme method");

}

}

class B extends A implements E

{

public void display()

{

System.out.println("This is in display method");

}

public void show()

{

System.out.println("This is in show method");

}

}

class C extends B

{

void call()

{

System.out.println("This is in call method");

}

}

class interfacedemo

{

public static void main(String args[])

{

C c1=new C();

c1.display();

c1.show();

c1.callme();

c1.call();

}

}


OUT-PUT:

This is in display method

This is in show method

This is in callme method

This is in call method


(iii) Third form of interface implementation


interface A

{

void display();

}

class B implements A

{

public void display()

{

System.out.println("This is in B's method");

}

}

class C implements A

{

public void display()

{

System.out.println("This is C's method");

}

}

class interfacedemo

{

public static void main(String args[])

{

B b1=new B();

C c1=new C();

b1.display();

c1.display();

}

}


OUT-PUT:

This is in B's method

This is C's method


(iv) Fourth form of interface implementation


interface A

{

void display();

}

interface B

{

void callme();

}

interface C extends A,B

{

void call();

}

class D implements C

{

public void display()

{

System.out.println("interface A");

}

public void callme()

{

System.out.println("interface B");

}

public void call()

{

System.out.println("interface C");

}

}

class interfacedemo

{

public static void main(String args[])

{

D d1=new D();

d1.display();

d1.callme();

d1.call();

}

}


OUT-PUT:

interface A

interface B

interface C

Comments