Inheritance and Method Overriding

  • View

  • Download

Embed Size (px)

Text of Inheritance and Method Overriding

  • Inheritance and Method Overriding


  • Inheritance and Method Overriding



    method Overriding

    Super Implement Java

  • Object Oriented Programming using Java

    1) Object Families2) Generalisation and Specialisation3) Inheritance4) Implementing Inheritance in Java5) Constructors6) Constructor Rules7) Access Control8) Abstract Classes9) Overriding Methods10) The Object Class11) Overriding toString() defined in Object

  • Object Families


    constructors and access control (public/private)

  • Generalisation and Specialisation

    Class 594602001



  • Inheritance

    (generalization and specialization)

    Class Object inheritance

    UML Class

  • Inheritance

    Book Magazine

  • Inheritance

  • Implementing Inheritance in Java


    Subclass superclass extends

    class MySubclass extends MySuperclass{// additional instance variables and// additional methods}

  • Constructors

    Class (Sub Super)

    Constructors Class


    Superclass superClass MySubClass extends MySuperClass{public MySubClass (sub-parameters){super(super-parameters);// other initialization}

  • Constructors

    Class Constructors

    Subclass public Publication (String pTitle, double pPrice, int pCopies){title = pTitle;// etc.}

    public Book (String pTitle, String pAuthor, double pPrice,int pCopies){super(pTitle, pPrice, pCopies);author = pAuthor;//etc.}

  • Constructor Rules

    superclass parameterless ()

    constructor subclass

  • Access Control

    protected Subclass

    private int copies;public int getCopies (){return copies;}public void setCopies(int pCopies){copies = pCopies;}

    superclass Private

    // in Bookpublic void orderCopies(int pCopies){setCopies(getCopies() + pCopies);}

    // and in Magazinepublic void recvNewIssue(String pNewIssue){setCopies(orderQty);currIssue = pNewIssue;}

  • Abstract Classes



    abstract class SuperClassName{// etc.}

  • Overriding Methods

    Subclass Superclass


    overriding method


    Subclass public class DiscMag extends Magazine{ // the constructorpublic DiscMag (String pTitle, double pPrice, int pOrderQt, String pCurrIssue, int pCopies){super(pTitle, pPrice, pOrderQty, pCurrIssue, pCopies);}// the overridden methodpublic void recvNewIssue(String pNewIssue){super.recvNewIssue(pNewIssue);System.out.println("Check discs attached to thismagazine");}}

  • The Object Class


    object Class

    new Class object

  • Overriding toString() defined in Object

  • Summary

    Inheritance superclass

    subclass code superclass subclasses

    code superclass subclasses Code

    constructors Subclass superclass Abstract override Subclass Java Class Object Object