PROGRAM – 11
Question: Write a program to calculate volume of boxes using
constructor overloading.
Input:
class Box {
double width, height, length;
Box(double w, double h, double l) {
width = w;
height = h;
length = l;
Box() {
width = 1;
height = 1;
length = 1;
Box(double len) {
width = len;
height = len;
length = len;
double volume() {
return (length * width * height);
}
}
public class Main {
public static void main(String[] args) {
Box box1 = new Box(1, 2, 3);
Box box2 = new Box();
Box box3 = new Box(5);
System.out.println("Volume of box1: " + box1.volume());
System.out.println("Volume of box2: " + box2.volume());
System.out.println("Volume of box3: " + box3.volume());
Output: //Janhvi Narayan
PROGRAM – 12
Question: Write a program to illustrate inheritance.
Input:
package com.company;
class Employee{
float salary=40000;
}
public class Programmer extends Employee {
int bonus=10000;
public static void main(String[] args){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output: //Janhvi Narayan
PROGRAM – 13
Question: Write a program to illustrate single inheritance.
Input:
package com.company;
class Animal1{
void eat(){
System.out.println("eating...");
}
}
class Dog1 extends Animal1{
void bark(){
System.out.println("barking...");
}
}
public class TestInheritance {
public static void main(String[] args){
Dog1 d=new Dog1();
d.bark();
d.eat();
}
}
Output: //Janhvi Narayan
PROGRAM – 14
Question: Write a program to illustrate multilevel inheritance.
Input:
package com.company;
class Animal2{
void eat(){System.out.println("eating...");}}
class Dog2 extends Animal2{
void bark(){System.out.println("barking...");}}
class BabyDog extends Dog2{
void smile(){System.out.println("smiling...");}}
class TestInheritance2{
public static void main(String[] args){
BabyDog d=new BabyDog();
d.smile();
d.bark();
d.eat();}}
Output: //Janhvi Narayan
PROGRAM – 15
Question: Write a program to illustrate hierarchical inheritance.
Input:
package com.company;
class Animal3{
void eat(){System.out.println("eating...");}}
class Dog3 extends Animal3{
void bark(){System.out.println("barking...");}}
class Cat3 extends Animal3{
void meow(){System.out.println("meowing...");}}
public class TestInheritance3 {
public static void main(String[] args){
Cat3 c=new Cat3();
c.meow();
c.eat(); }}
Output: //Janhvi Narayan