Write a JAVA Programs For Interface And Inheritance.
Ex: No: 12 INTERFACES AND INHERITANCE IN JAVA
Aim:To write a JAVA programs for interface and inheritance.
Algorithm:
Step 1: Start the programs.
Step 2: Create and interface named sports which contains member function.
Step 3: Created three classes are inherited.
Step 4: Created an object for the class result.
Step 5: With respect to create object make a function call to the function on other classes including interface.
Step 6: Stop the program.
Programs:
//Interface program
interface Area
{
float compute(float x, float y);
}
//Inheritance program
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
Output:
Area of a rectangle=2.0
Area of a triangle=10.0
Result:
Thus the implementation of JAVA program for interface and inheritance is executed and the output has been verified.
//Interface program
interface Area
{
float compute(float x, float y);
}
//Inheritance program
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
Output:
Area of a rectangle=2.0
Area of a triangle=10.0
Result:
Thus the implementation of JAVA program for interface and inheritance is executed and the output has been verified.
0 comments:
Post a Comment