2-BTECH-CSE:: I SEM
OOPS THROUGH JAVA PROGRAMMING LAB
EXPERIMENT: 5
5(a)AIM:: Write a JAVA program give example for “super” keyword.
class Person
{
int id;
String name;
Person(int id,String name)
{
[Link]=id;
[Link]=name;
}
}
class Emp extends Person
{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
[Link]=salary;
}
void display(){[Link](id+" "+name+" "+salary);}
}
class SuperExample
{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
[Link]();
}}
Output:
1 ankit 45000
5b) Write a JAVA program to implement Interface. What kind of
Inheritance can be achieved?
interface Polygon
{
void getArea();
default void getSides()
{
[Link]("I can get sides of a polygon.");
}
}
class Rectangle implements Polygon
{
public void getArea()
{
int length = 6;
int breadth = 5;
int area = length * breadth;
[Link]("The area of the rectangle is " + area);
}
public void getSides()
{
[Link]("I have 4 sides.");
}
}
class Square implements Polygon
{
public void getArea() {
int length = 5;
int area = length * length;
[Link]("The area of the square is " + area);
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
[Link]();
[Link]();
Square s1 = new Square();
[Link]();
[Link]();
}
}
Output
The area of the rectangle is 30
I have 4 sides.
The area of the square is 25
I can get sides of a polygon.
5c) Write a JAVA program that implements Runtime polymorphism
class Shape
{
void draw()
{
[Link]("drawing...");
}
}
class Rectangle extends Shape
{
void draw()
{
[Link]("This is a rectangle...");}
}
class Circle extends Shape
{
void draw()
{
[Link]("This is a circle...");}
}
class Triangle extends Shape
{
void draw()
{
[Link]("This is a triangle...");}
}
class PrepBytes
{
public static void main(String args[])
{
Shape s;
s=new Rectangle();
[Link]();
s=new Circle();
[Link]();
s=new Triangle();
[Link]();
}
}
Output:
This is a rectangle...
This is a circle...
This is a triangle...