Week05

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

WEEK : 5

List of Programs:
1. Write a java program to implement simple inheritance.
2. Write a java program to implement multilevel inheritance.
3. Write a java program to implement hierarchical inheritance.
4. Write a java program to implement aggregation.
5. Write a java program to implement method overriding.
6. Write a java program to show the use of super keyword in inheritance.
7. Write a java program to show constructor calling in multilevel inheritance.
8. Write a java program to implement dynamic method dispatch concept.

21
Program 1. Write a java program to implement simple inheritance.

SOURCE CODE:

class A {
int i, j;
void showij() {
System.out.println("iand j:" + i + j);
}}
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k:" + (i + j + k));
}}
class SimpleInheritance {
public static void main(String args[]) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}}

OUTPUT:

22
Program 2. Write a java program to implement multilevel inheritance.

SOURCE CODE:

class Animal {
void eat() {
System.out.println("eating...");
}}

class Dog extends Animal {


void bark() {
System.out.println("barking...");
}}

class BabyDog extends Dog {


void weep() {
System.out.println("weeping...");
}}

class TestInheritance2 {
public static void main(String args[]) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
BabyDog d = new BabyDog();
d.weep();
d.bark();
d.eat();
}}

OUTPUT:

Program 3. Write a java program to implement hierarchical inheritance.

SOURCE CODE:

class Animal {
void eat() {
System.out.println("eating...");
}}

class Dog extends Animal {

23
void bark() {
System.out.println("barking...");
}}

class Cat extends Animal {


void meow() {
System.out.println("meowing...");
}}

class TestInheritance3 {
public static void main(String args[]) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
Cat c = new Cat();
c.meow();
c.eat();
}}

OUTPUT:

Program 4. Write a java program to implement aggregation.

SOURCE CODE:

class Address {
String city, state, country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}}

class Emp {
int id;
String name;
Address address;
public Emp(int id, String name, Address address){
this.id = id;
this.name = name;
this.address = address;
}
void display() {

24
System.out.println(id + " " + name);
System.out.println(address.city + " " + address.state + " " + address.country);
}}

class TesAgg{
public static void main(String[] args) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
Address address1 = new Address("gzb", "UP", "india");
Address address2 = new Address("gno", "UP", "india");
Emp e = new Emp(111, "varun", address1);
Emp e2 = new Emp(112, "arun", address2);
e.display();
e2.display();
}}

OUTPUT:

Program 5. Write a java program to implement method overriding.

SOURCE CODE:

class Bank {
int getRateOfInterest() {
return 0;
}}
class SBI extends Bank {
int getRateOfInterest() {
return 8;
}}
class ICICI extends Bank {
int getRateOfInterest() {
return 7;
}}
class AXIS extends Bank {
int getRateOfInterest() {
return 9;
}}
class Test2 {
public static void main(String args[]) {
Bank b;
SBI s = new SBI();
b = s;

25
b.getRateOfInterest();
ICICI i = new ICICI();
b = i;
b.getRateOfInterest();
AXIS a = new AXIS();
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
System.out.println("SBI Rate of Interest: " + s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: " + i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: " + a.getRateOfInterest());
}}

OUTPUT:

Program 6. Write a java program to show the use of super keyword in inheritance.

SOURCE CODE:

class Person {
int id;
String name;
Person(int id, String name) {
this.id = id;
this.name = name;
}}

class Empl extends Person {


float salary;
Empl(int id, String name, float salary) {
super(id, name);
this.salary = salary;
}

void display() {
System.out.println(id + " " + name + " " + salary);
}}

class TestSuper5 {
public static void main(String[] args) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
Empl e1 = new Empl(1, "ankit", 45000f);
e1.display();

26
}}

OUTPUT:

Program 7. Write a java program to show constructor calling in multilevel inheritance.

SOURCE CODE:

class A {
A() {
System.out.println("Inside A's constructor.");
}}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}}
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}}
class CallingCons {
public static void main(String args[]) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
C c = new C();
}}

OUTPUT:

27
Program 8. Write a java program to implement dynamic method dispatch concept.

SOURCE CODE:

class X {
void callme() {
System.out.println("Inside X's callme method");
}
}

class Y extends X {
void callme() {
System.out.println("Inside Y's callme method");
}
}

class Z extends X {
void callme() {
System.out.println("Inside Z's callme method");
}
}

class Dispatch {
public static void main(String args[]) {
System.out.println("\nName: Paras Garg \t Roll no: 22103116\n");
X a = new X();
Y b = new Y();
Z c = new Z();
X r;
r = a;
r.callme();
r = b;
r.callme();
r = c;
r.callme();
}
}

OUTPUT:

28

You might also like