0% found this document useful (0 votes)
9 views22 pages

M3 Multilevelhirarchy

Uploaded by

C.M Srinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

M3 Multilevelhirarchy

Uploaded by

C.M Srinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

M3-Inheritance

Creating a Multilevel Hierarchy


class A {
void funcA() {
System.out.println("This is class A"); } }
class B extends A {
void funcB() { System.out.println("This is class B");
} }
class C extends B { void funcC() {
System.out.println("This is class C"); } }
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
obj.funcC();
} }
When Constructors Are Executed
• // Create a super class.
class A {
A() { System.out.println("Inside A's constructor."); } }
// Create a subclass by extending class A.
class B extends A {
B() { System.out.println("Inside B's constructor."); } }
// Create another subclass by extending B.
class C extends B {
C() { System.out.println("Inside C's constructor."); } }
• class CallingCons {
• public static void main(String[] args) {
• C c = new C();
• }
• }
• Output:
• The output from this program is shown here:
• Inside A's constructor
• Inside B's constructor
• Inside C's constructor
• Note: the constructors are executed in order of derivation.
Method- Overriding
• Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one
of its super-classes or parent classes.
• Method overriding occurs only when the names and the type
signatures of the two methods are identical.
• Method overriding is one of the ways by which Java achieves
Run Time Polymorphism.
Java program to demonstrate method overriding in java(RUN TIME
POLYMORPHISM)
// Base Class
class Parent {
void show()
{ System.out.println("Parent's show()"); } }
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
void show() {
System.out.println("Child's show()"); } }
// Driver class
class Main { public static void main(String[] args) {
// If a Parent type reference refers to a Parent object, then Parent's show is called
Parent obj1 = new Parent();
obj1.show();
// If a Parent type reference refers to a Child object Child's show() is called.
This is called RUN TIME POLYMORPHISM.
Parent obj2 = new Child();
obj2.show(); } }
Create three sub classes
namely: circle, triangle and square, each class has two
member functions named
draw () and erase (). Demonstrate polymorphism concepts
by developing suitable
methods, defining member data and main program.
• class Shape { // Member functions
• public void draw()
• { System.out.println("Drawing a shape"); }
• public void erase() {
• System.out.println("Erasing a shape"); }}
• // Circle class, a subclass of Shape
• class Circle extends Shape {
• public void draw1() { System.out.println("Drawing a circle"); }
• public void erase1() { System.out.println("Erasing a circle"); }}
• // Triangle class, a subclass of Shape
• class Triangle extends Shape {
• public void draw2() { System.out.println("Drawing a triangle"); }
public void erase2() { System.out.println("Erasing a triangle"); }}
• class Square extends Shape{
• public void draw3() {
• System.out.println("Drawing a square"); }
• public void erase3() { System.out.println("Erasing a square"); }}
• // Main program to demonstrate polymorphism
• class Main{
• public static void main(String[] args) {
• // Creating objects of different shapes
• Circle c = new Circle();
• Triangle t = new Triangle();
• Square s = new Square(); // Demonstrating polymorphism by calling
//draw and erase methods
• System.out.println("Using Circle object:"); c.draw(); c.erase();
System.out.println("\nUsing Triangle object:"); t.draw(); t.erase();
System.out.println("\nUsing Square object:"); s.draw(); s.erase(); }}
OUTPUT
• Using Circle object:
• Drawing a shape
• Erasing a shape

• Using Triangle object:


• Drawing a shape
• Erasing a shape

• Using Square object:


• Drawing a shape
• Erasing a shape
Dynamic Method Dispatch
• Method overriding forms the basis for one of Java’s most powerful
concepts: dynamic method dispatch.
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch is important because this is how Java
implements run-time polymorphism.
• When different types of objects are referred to, different versions of
an overridden method will be called.
• if a superclass contains a method that is overridden by a subclass,
then when different types of objects are referred to through a
superclass reference variable, different versions of the method are
executed.
/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");
• }
• }
• // Driver class
• class Dispatch
• {
• public static void main(String args[])
• {
• // object of type A
• A a = new A();
• // object of type B
• B b = new B();
• // object of type C
• C c = new C();
• // obtain a reference of type A
• A ref;
• // ref refers to an A object
• ref = a;
• // calling A's version of m1()
• ref.m1();
• // now ref refers to a B object
• ref = b;
• // calling B's version of m1()
• ref.m1();
• // now ref refers to a C object
• ref = c;
• // calling C's version of m1()
• ref.m1();
• }
OUTPUT:
Inside A's m1 method
Inside B's m1 method
Inside C's m1 method
• Now a reference of type A, called ref, is also declared, initially it will
point to null.
• A ref; // obtain a reference of type A

• ref = a; // r refers to an A object ref.m1(); // calling A's version of m1()



Why Overridden Methods? Or advantages
• overridden methods allow Java to support run-time polymorphism.
• Polymorphism is essential to object-oriented programming for one reason:
it allows a general class to specify methods that will be common to all of its
derivatives, while allowing subclasses to define the specific
implementation of some or all of those methods.
• Overridden methods are another way that Java implements the “one
interface, multiple methods” aspect of polymorphism.
• by combining inheritance with overridden methods, a superclass can
define the general form of the methods that will be used by all of its
subclasses.
• Dynamic, run-time polymorphism is one of the most powerful mechanisms
that object oriented design brings to bear on code reuse and robustness.
// Using run-time polymorphism.

• class Figure {
• double dim1; double dim2;
• Figure(double a, double b)
• { dim1 = a; dim2 = b; }
• double area()
• { System.out.println("Area for Figure is undefined.");
• return 0; } }
• class Rectangle extends Figure {
• Rectangle(double a, double b) { super(a, b); }
• // override area for rectangle
• double area() {
• System.out.println("Inside Area for Rectangle.");
• return dim1 * dim2; } }
• class Triangle extends Figure {
• Triangle(double a, double b) {
• super(a, b); }
• // override area for right triangle
• double area() {
• System.out.println("Inside Area for Triangle.");
• return dim1 * dim2 / 2; } }
• class FindAreas { public static void main(String[] args)
• {
• Figure f = new Figure(10, 10);
• Rectangle r = new Rectangle(9, 5);
• Triangle t = new Triangle(10, 8);
• Figure figref;
• figref = r;
• System.out.println("Area is " + figref.area());
• figref = t;
• System.out.println("Area is " + figref.area());
• figref = f;
• System.out.println("Area is " + figref.area());
• }
• }
The output from the program is
shown here:
• Inside Area for Rectangle.
• Area is 45 Inside Area for Triangle.
• Area is 40 Area for Figure is undefined. Area is 0

You might also like