4 inheritance

Upload: shakti-rathore

Post on 06-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 4 Inheritance

    1/21

  • 8/3/2019 4 Inheritance

    2/21

    InheritanceInheritance is the process by which one object acquires the

    properties of another object.

    This is important because it support the concept ofhierarchical classification.

    Using inheritance, we can create a general class that definescommon set of features/related items.

    To inherit a class, incorporate the definition of one class intoanother by using the extends keyword.

  • 8/3/2019 4 Inheritance

    3/21

    Type of Inheritance

    A

    B

    C

    B

    A

    C

    BA

    1) Single Level

    3) Multiple

    2) Multi Level

    X interface

    subclass

    superclass

  • 8/3/2019 4 Inheritance

    4/21

    y

    Java does not support the inheritance of multiple superclasses into a single subclass.

    y Subclass include all of the members of its super class, it

    cannot access those members of the super class that hasbeen declared as private.

  • 8/3/2019 4 Inheritance

    5/21

    class A{int x;public void setInfo() {

    x=5;}public void printInfo() {

    System.out.println(x);

    }}class B extends A{

    int y;public void setInfo1() {

    x=10;y=10;

    }public void printInfo1() {

    System.out.prinltn(x);System.out.println(y);}

    }class Test {

    public static void main(String args[]) {Aa= newA();a.setInfo();a.printInfo();a.setInfo1(); // X cannot access subclass method or instance variablea.printInfo1(); // X

    B b= new b();b.setInfo();b.printInfo();b.setInfo1();b.printInfo1();

    }}

  • 8/3/2019 4 Inheritance

    6/21

    When constructors are calledclassA{

    A() {

    System.out.println(I am in A.);}

    }classB extendsA{

    B() {System.out.println(I am in B.);

    }}classC extendsB {

    C() {System.out.println(I am in C.);

    }

    }classABCTest {public static void main(String args[]) {

    new C();}

    }

  • 8/3/2019 4 Inheritance

    7/21

    Output 3 object created

    I am in A.

    I am in B.

    I am in C.

    Process

    new C()goes to C class but not createC object and goes to Bclass but not create B object and goes to Aclass and thencreate Aobject after that B, the C object.

  • 8/3/2019 4 Inheritance

    8/21

    Constructors are executed in order of derivation. Because asuperclass has no knowledge of any subclass.

    Destructors work in order to subclass to superclass/ lower tohigher.

    A

    B

    C

    start

    End

  • 8/3/2019 4 Inheritance

    9/21

    SuperThe keyword super provides a reference of the current object as an instance of

    its superclass.

    Super has two general forms.1) The first calls the superclass constructor.2) The second is used to access a member of the superclass hat has been

    hidden by a member of a subclass.

    Super() must always be the first statement executed inside a subclassconstructor.

    When a subclass calls super(), it is calling the constructor of its immediatesuperclass in a multileveled hierarchy.

    If you wish to access the superclass version of an overridden method. You can doby using super.

  • 8/3/2019 4 Inheritance

    10/21

    class Box {

    private double width, height, depth;

    Box() {

    width=1; height=1; depth=1;

    }

    Box(double l){

    width=l; height=l; depth=l;

    }

    Box(double w, double h, double d) {

    width=w; height=h; depth=d;

    }

    Box(Box ob) {

    width=ob.width;

    height=ob.height;

    depth=ob.depth;}

    public double calVolume() {

    return width*height*depth;

    }

    }

  • 8/3/2019 4 Inheritance

    11/21

    classBoxWeight extendsBox {

    double weight;

    BoxWeight(double w, double h, double d, double we) {

    super(w, h, d);

    weight=we;}

    BoxWeight() {

    super();

    weight=1;

    }

    BoxWeight(double l) {

    super(l);

    weight=l;

    }

    BoxWeight(BoxWeight bw) {

    super(bw);

    weight=bw.weight;

    }

    doubleBox_Weight() {

    return weight;

    }

    }

  • 8/3/2019 4 Inheritance

    12/21

    class SuperTest {

    public static void main(String args[]) {

    BoxWeight bw = new BoxWeight(5, 6, 7, 8);System.out.println(volume is = +bw.calVolume());

    BoxWeight bw1 = new BoxWeight(5);

    System.out.println(volume is = +bw1.calVolume());

    BoxWeight bw2 = new BoxWeight(bw);

    System.out.println(volume is = +bw2.calVolume());

    }}

  • 8/3/2019 4 Inheritance

    13/21

    Using super to overcome name hiding.classA{

    int I;

    }

    classB extendsA{int i; //this I hides the I in A

    B(int a, int b) {

    super.i=a; // I in A

    i=b; // I in B

    }void show() {

    System.out.println(I in super class: +super.i);

    System.out.println(I in subclass: +i);

    }

    }

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

    B subOB = new B(101, 321);

    subOB.show();

    }

    }

  • 8/3/2019 4 Inheritance

    14/21

    Method OverridingWhen a method in a subclass has the same name and

    signature type as a method in its superclass, then themethod in the subclass is said to override the methodin the superclass.

    When an overridden method is called from within asubclass, it will always refer to the version of thatmethod defined by the superclass .

    The version of the method defined by the superclasswill be hidden. If you wish to access the superclassversion of an overridden method, you can do so byusing super.

  • 8/3/2019 4 Inheritance

    15/21

    Rule of method overridingThe compulsory condition of method overriding is same function name,

    signature (no. of parameters, data type) and return type.

    Access specifier of new method definition cannot narrow, but it can widen it

    Super keyword is used to access the method or instance variable of parent classin child class.

    We call super any where in method but not in constructor

    An instance method in a subclass cannot override a static method in thesuperclass.The compiler will f lag this as an error.A

    static method is class-specific and part of any object, while overridingmethod are invoked on behalf of the object of subclass.

    However, a static method in a subclass can hide a static method in thesuperclass.

  • 8/3/2019 4 Inheritance

    16/21

    class Movie {

    public void start() {

    System.out.println(Welcome to the Movie);

    }

    public void interval() {

    System.out.println(Coca-cola is only for us);

    }

    public void end() {

    System.out.println(Thanks for coming);

    }

    }

    class Picture extends Movie {

    public void reel1() {

    System.out.println(This is reel1);

    }public void interval() {

    System.out.println(Pepsi in only for us);

    // super.interval();

    }

    public void reel2() {

    System.out.println(This is reel2);

    }

    }

    class OverridingTest {

    public static void main(String args[]) {

    Picture p= new Picture();

    p.start();

    p.reel1();

    p.interval();

    p.reel2();

    p.end();

    }}

  • 8/3/2019 4 Inheritance

    17/21

    Runtime PolymorphismDynamic method binding is the mechanism by which call to an

    overridden method is resolved at runtime, rather than compiletime.

    Principle

    Asuperclass reference variable can refer to a subclass object.

    Java uses this fact to resolve calls to overridden method atruntime .

    When an overridden method is called through a superclassreference, Java determines which version of that method toexecute based upon the type of the object being referred to at the

    time the call occurs.

  • 8/3/2019 4 Inheritance

    18/21

    RuleA. parent-child relation should be exist

    B. Whatever the method you are calling by the behalf of parentclass object that method should be defined in parent class, ifit is not define then compile error.

    C. What ever the reference hold by the parent, call the methodof that class. If it is not found then it will call the parent classmethod.

  • 8/3/2019 4 Inheritance

    19/21

    class Figure {

    int x, y;

    Figure() {

    x=5; y=6;

    }

    public void area() {

    System.out.println(This is Figure of area);

    }

    }

    class Rectangle extends Figure {

    Rectangle() {

    super();}

    public void area() {

    System.out.println(Area of Rectangle is +(x*y));

    }

    }

    class Triangle extends Figure {Triangle() {

    super();

    }

    public void area() {

    System.out.println(Area of Triangle is +(x*y)/z);

    }}

  • 8/3/2019 4 Inheritance

    20/21

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

    Figure f= new Figure();Rectangle r= new Rectangle();Triangle t= new Triangle();f.area();

    r.area();t.area();

    Figure f1;f1=f;f1.area();f1=r;

    f1.area();f1=t;f1.area();Rectangle r1;r1=f; //errorr1.area();r1=r;r1.area();r1=t; //erroer1.area();

    }}

  • 8/3/2019 4 Inheritance

    21/21

    Overriding vs. Overloading

    Method overriding requires the same method signature (name and

    parameters) and the same return type. Overloading occurs when the method names are the same, but the parameter

    lists differ.

    Amethod can be overloaded in the class it is defined or in a subclass of its

    class.

    Invoking an overridden method in the superclass from a subclass requiresspecial syntax ex:- the keyword super.

    This is not necessary for invoking an overloaded method in the superclassfrom a subclass. If the right kinds of arguments are passed in the method calloccurring in the subclass, the overloaded method in the superclass will beinvoked.