Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Inheritance Example 3- Room - Java

Program:
class Room
{
   int length,width;
   Room(int a,int b)
   {
     length = a;
     width = b;
   }
   void area()
   {
     int area = length*width;
     System.out.println("The area of the room is " +area);
   }
}

class roomvol extends Room
{
   int height;
   roomvol(int a,int b,int c)
   {
     super(a,b);
     height = c;
   }
   void volume()
   {
     int volume = length*width*height;
     System.out.println("The volume of the room is " +volume);
   }
}

class inheritance3
{
   public static void main(String args[])
   {
     roomvol room2 = new roomvol(10,40,20);
     room2.area();
     room2.volume();
   }
}

Output:
nn@linuxmint ~ $ javac inheritance3.java
nn@linuxmint ~ $ java inheritance3
The area of the room is 400
The volume of the room is 8000
nn@linuxmint ~ $

Inheritance Example 2 - Figure - Java

Program:
class figure
{
    int b;
    int l;
    figure(int x,int y)
    {
        l=x;
        b=y;
    }
}
class triangle extends figure
{
    triangle (int x,int y)
    {
        super(x,y);
    }
    int show()
    {
        return(l*b/2);
    }
}
class rectangle extends figure
{
    rectangle(int x,int y)
    {
        super(x,y);
    }
    int show()
    {
        return(l*b);
    }
}

class inheritance2
{
public static void main(String argrs[])
{
    figure fig1=new figure(4,5);
    triangle tri1=new triangle(4,5);
    rectangle rect1= new rectangle(4,5);
    System.out.println("Area of triangle="+tri1.show());
    System.out.println("Area of rectangle="+rect1.show());
}
}

Output:
nn@linuxmint ~ $ javac inheritance2.java
nn@linuxmint ~ $ java inheritance2
Area of triangle=10
Area of rectangle=20
nn@linuxmint ~ $

Inheritance Example 1- Employee - Java

Program:
class employe
{
    protected int emp_no;
    protected String name;
    protected int salary;
    public employe(int empno,String nam,int sal)
    {
        emp_no=empno;
        name=nam;
        salary=sal;
    }
    public void emplo_data()
    {
        System.out.println("\nEmploy no.="+emp_no); 
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
    }
}
class manager extends employe
{
    int reward;
    public manager(int empno,String nam,int sal,int p)
    {
        super(empno,nam,sal);
        reward=p;
    }
    public void managerdata()
    {
        System.out.println("\nEmploy no.="+emp_no);
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
        System.out.println("Reward="+reward);
    }
}
class scientist extends employe
{
    int perks;
    public scientist(int empno,String nam,int sal,int s)
    {
        super(empno,nam,sal);
        perks=s;
    }
    public void scientistdata()
    {
        System.out.println("\nEmploy no.="+emp_no);
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
        System.out.println("Perks="+perks);
    }
}
class inheritance
{
public static void main(String args[])
{
    employe emp= new employe(1,"Varun",20000);
    emp.emplo_data();
    manager man= new manager(2,"Arun",50000,1000);
    man.managerdata();
    scientist scient= new scientist(3,"Nithin",60000,5000);
    scient.scientistdata();
}
}

Output:
nn@linuxmint ~ $ javac inheritance.java
nn@linuxmint ~ $ java inheritance

Employ no.=1
Name=Varun
Salary=20000

Employ no.=2
Name=Arun
Salary=50000
Reward=1000

Employ no.=3
Name=Nithin
Salary=60000
Perks=5000
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...