Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Oop Example - Inheritance

Inheritance
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Oop Example - Inheritance

Inheritance
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.//Order of constructor execution in Multilevel inheritance..

class College
{ College() /* default Constructor */
{ System.out.println("College constructor executed");
}
}

class Department extends College


{ Department() /* Constructor */
{ System.out.println("Department constructor executed");
}
}

class Student extends Department


{ Student() /* Constructor */
{ System.out.println("Student constructor executed");
}
}
public class Main
{ public static void main(String ar[])
{ /* Create instance of Student class */
System.out.println("Order of constructor execution in Multilevel inheritance...");
Student s=new Student();
}
}
Output:
Order of constructor execution in Multilevel inheritance...
College constructor executed
Department constructor executed
Student constructor executed

2.Calling superclass constructor using super keyword


class Parent /* Parent Class */
{ int a;
Parent(int x)
{ a = x;
}
}
class Child extends Parent /* Child Class */
{ int b;
Child(int x, int y)
{ super(x); /* Accessing Parent Constructor */
b = y;
}

void Show() /* Method to show value of a and b */


{ System.out.println("Value of a : "+a+"\nValue of b : "+b);
}
}

public class OrderofExecution4


{ public static void main(String ar[])
{ System.out.println("Order of constructor execution...");
Child d = new Child(79, 89);
d.Show();
} }
Output: Order of constructor execution...
Value of a : 79
Value of b : 89

3.// Inheritance with constructor overloading


class Parent {
int x;
public Parent() {
System.out.println("parent:default constructor");
}
public Parent(int x) {
this.x = x;
System.out.println("Parent parameter constructor called with x = " + x);
}
}

class Child extends Parent {


int y;
public Child() {
System.out.println("child:default constructor");
}
public Child(int x, int y) {
super(x); // Call the parameterized constructor of Parent
this.y = y;
System.out.println("Child parameter constructor called with y = " + y);
}
}

public class Main {


public static void main(String[] args) {
Child child1 = new Child();
Child child2 = new Child(10, 20);
}
}
Output:
parent:default constructor
child:default constructor
Parent parameter constructor called with x = 10
Child parameter constructor called with y = 20

// A Java program to illustrate Dynamic Method Dispatch using hierarchical inheritance


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

class B extends A
{ // overriding m1()
void m1()
{ System.out.println("Inside B's m1 method");
}
}

class C extends A
{ // overriding m1()
void m1()
{ System.out.println("Inside C's m1 method");
}
}
class Dispatch // Driver class
{ public static void main(String args[])
{ A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C

A ref=a; // obtain a reference of type A


ref.m1(); // calling A's version of m1()

B ref=b; // obtain a reference of type A


ref.m1(); // calling B's version of m1()

A ref=c; // obtain a reference of type A


ref.m1(); // calling C's version of m1()
}
}
Output:
Inside A's m1 method
Inside B's m1 method
Inside C's m1 method
Explanation :

You might also like