0% found this document useful (0 votes)
2 views3 pages

Task 4 Adsa

The document contains three Java programs demonstrating different types of inheritance and the use of abstract classes. The first program illustrates single inheritance with an Animal class and a Dog subclass. The second program showcases multi-level inheritance with Animal, Cat, and Babycat classes, while the third program uses an abstract Shape class to calculate areas of Rectangle and Triangle classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Task 4 Adsa

The document contains three Java programs demonstrating different types of inheritance and the use of abstract classes. The first program illustrates single inheritance with an Animal class and a Dog subclass. The second program showcases multi-level inheritance with Animal, Cat, and Babycat classes, while the third program uses an abstract Shape class to calculate areas of Rectangle and Triangle classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

a.

Write a JAVA program to implement Single Inheritance

class Animal
{
public void getName(String name)
{
System.out.println("Name of the Animal"+name);
}
}
class Dog extends Animal
{
public void getBreed(String breedName)
{
System.out.println("Breed Name of the Dog\t"+breedName);
}
}
class MainDemo
{
public sta c void main(String args[])
{
Dog d=new Dog();
d.getName("dog");
d.getBreed("puppy");
}}

b. Write a JAVA program to implement mul level Inheritance

class Animal
{
void display1()
{
System.out.println("Ea ng.....");
}
}
class Cat extends Animal
{
void display2()
{
System.out.println("Meow....");
}
}
class Babycat extends Cat
{
void display3()
{
System.out.println("Crying....");
}
}
public class Main{
public sta c void main(String[] args)
{
Babycat bc =new Babycat();
bc.display1();
bc.display2();
bc.display3();
}
}

c. Write a JAVA program for abstract class to find areas of different shapes

abstract class Shape


{
int dim1,dim2;
Shape(int x,int y)
{
dim1=x;
dim2=y;
}
abstract void area();
}
class Rectangle extends Shape
{
Rectangle(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println("Area of Rectangle is :"+(dim1*dim2));
}
}
class Triangle extends Shape
{
Triangle(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println("Area of Triangle is :"+(dim1*dim2)/2);
}
}
class Test
{
public sta c void main(String args[])
{
Shape s;
Rectangle rect=new Rectangle(10,20);
s=rect;
s.area();
Triangle tri=new Triangle(10,20);
s=tri;
s.area();
}
}

You might also like