Java Programming Practical 8

Question 1
Write a program to implement single inheritance
SingleInheritanceDemo.java
Java
				class Person 
{
    String name;

    public void get(String n) 
	{
        name=n;
    }

    public void show() 
	{
        System.out.println("Name is: " +name);
    }
}

class Student extends Person 
{
    int rollno;

    public void accept(int r) 
	{
        rollno=r;
    }

    public void display() 
	{
        System.out.println("Roll no is: " +rollno);
    }
}

public class SingleInheritanceDemo 
{
    public static void main(String[] args) 
	{
        Student s = new Student();
		s.get("Suresh");
        s.accept(47);
		s.show();
        s.display();
    }
}
			
Output
Question 2
Write a program to implement multilevel inheritance.
MultilevelInheritanceDemo.java
Java
				class Person 
{
	String name;
	void getPerson(String n)
	{
		name=n;
	}
    void displayPerson() 
	{
        System.out.println("Name is: "+name);
    }
}

class Employee extends Person 
{
	int id;
	void getEmp(int i)
	{
		id=i;
	}
    void displayEmployee() 
	{
        System.out.println("ID no. is: "+id);
    }
}

class Programmer extends Employee 
{
	String skill;
	void getProgrammer(String s)
	{
		skill=s;
	}
    void displayProgrammer() 
	{
        System.out.println("Skill language is: "+skill);
    }
}

public class MultilevelInheritanceDemo 
{
    public static void main(String[] args) 
	{
        Programmer obj = new Programmer();
		obj.getPerson("ABC");
		obj.getEmp(4545);
		obj.getProgrammer("Java");
        obj.displayPerson(); 
        obj.displayEmployee(); 
        obj.displayProgrammer(); 
    }
}
			
Output
Question 3
Develop a program to calculate he room area and volume to illustrate the concept of single inheritance.
RoomDemo.java
Java
				class Room 
{
    int length, width;

    Room(int l, int w) 
	{
        length = l;
        width = w;
    }

    void calculateArea() 
	{
		int a=length * width;
		System.out.println("Room Area: " + a + " square units");
    }
}

class RoomVolume extends Room 
{
    int height;

    RoomVolume(int l, int w, int h) 
	{
        super(l, w);
        height = h;
    }

    void calculateVolume() 
	{
        int v=length*width*height;
		System.out.println("Room Volume: " + v + " cubic units");
    }
}

public class RoomDemo 
{
    public static void main(String[] args) 
	{
        RoomVolume myRoom = new RoomVolume(10, 12, 8);
        myRoom.calculateArea();
		myRoom.calculateVolume();
    }
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *