Exercise - 6 (Inheritance - Continued)
a). Write a JAVA program give example for “super” keyword.
//Write a program to implement 'super' keyword
class One
{
protected int a;
One(int i)
{
this.a=i;
}
void display()
{
System.out.println("Super class method:a="+a);
}
}
class Two extends One
{
protected int a;
Two(int i,int j)
{
super(i);
a=j;
}
void display()
{
super.display();
System.out.println("Sub class method:a="+a);
}
}
class Super1
{
public static void main(String args[])
{
Two t=new Two(9,6);
t.display();
}
}
b). Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?
mport java.util.Scanner;
interface Base1
{
final double pie=3.14;
void area();
}
class Base2
{
protected int r;
public void getdata()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the radius of the Circle:");
r=s.nextInt();
}
}
class Derived extends Base2 implements Base1
{
double area;
public void area()
{
area=pie*r*r;
System.out.println("Area of the Circle is:"+area);
}
}
class Inheritance
{
public static void main(String args[])
{
Derived d=new Derived();
d.getdata();
d.area();
}
}
Multiple inheritance is achieved through interfaces
No comments:
Post a Comment