0% found this document useful (0 votes)
52 views6 pages

Java Inheritance & Overloading

Uploaded by

janhvilkw
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)
52 views6 pages

Java Inheritance & Overloading

Uploaded by

janhvilkw
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

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);

[Link]("Volume of box1: " + [Link]());

[Link]("Volume of box2: " + [Link]());

[Link]("Volume of box3: " + [Link]());

Output: //Janhvi Narayan


PROGRAM – 12
Question: Write a program to illustrate inheritance.
Input:
package [Link];
class Employee{
float salary=40000;
}
public class Programmer extends Employee {
int bonus=10000;
public static void main(String[] args){
Programmer p=new Programmer();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}

Output: //Janhvi Narayan


PROGRAM – 13
Question: Write a program to illustrate single inheritance.
Input:
package [Link];
class Animal1{
void eat(){
[Link]("eating...");
}
}
class Dog1 extends Animal1{
void bark(){
[Link]("barking...");
}
}
public class TestInheritance {
public static void main(String[] args){
Dog1 d=new Dog1();
[Link]();
[Link]();
}
}

Output: //Janhvi Narayan


PROGRAM – 14
Question: Write a program to illustrate multilevel inheritance.
Input:
package [Link];

class Animal2{

void eat(){[Link]("eating...");}}

class Dog2 extends Animal2{


void bark(){[Link]("barking...");}}

class BabyDog extends Dog2{

void smile(){[Link]("smiling...");}}

class TestInheritance2{
public static void main(String[] args){

BabyDog d=new BabyDog();

[Link]();

[Link]();

[Link]();}}

Output: //Janhvi Narayan


PROGRAM – 15
Question: Write a program to illustrate hierarchical inheritance.
Input:
package [Link];

class Animal3{

void eat(){[Link]("eating...");}}

class Dog3 extends Animal3{


void bark(){[Link]("barking...");}}

class Cat3 extends Animal3{

void meow(){[Link]("meowing...");}}

public class TestInheritance3 {


public static void main(String[] args){

Cat3 c=new Cat3();

[Link]();

[Link](); }}

Output: //Janhvi Narayan

You might also like