7. JAVA InheritanceAndInterfaces

Embed Size (px)

Citation preview

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    1/41

    Inheritance and Interfaces

    One of the most important concepts of OOP

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    2/41

    Module Introduction

    Inheritance

    Constructor Inheritance

    Overriding Methods

    Overloading of methods

    abstract class

    Using final keyword

    Interfaces

    FPT- APTECH 2/41FPT- APTECH 2/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    3/41

    #1 - Inheritance

    Explain the concept of inheritance

    State the purpose of method overriding

    State the use of a super keyword.

    FPT- APTECH 3/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    4/41

    What is Inheritance?

    The process, whereby characteristics and behavior are transmittedfrom a parent to a child entity, is called inheritance.

    Create a new class from the existing class.reuse the fields and methods

    FPT- APTECH 4/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    5/41

    Basic concepts of Inheritance

    SuperclassThe class from which the subclass is derived is called a

    Base class or parent class.

    Subclass

    Class that is derived from another classDerived class, extended class, or child class

    FPT- APTECH 5/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    6/41

    Inheritance basic concepts cont

    Use extends keyword to create a subclass.

    A class can be directly derived from only one class (singleinheritance)

    If a class does not have any superclass, then it is implicitly derivedfrom Object class.Object class is parent of all Java classes

    A subclass can inherit all the protected members of itssuperclass.

    Constructors are not inherited by subclassesThe constructor of the superclass can be invoked from the subclass.

    FPT- APTECH 6/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    7/41

    Example of Inheritance

    FPT- APTECH 7/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    8/41

    super keyword

    Use to access superclasss members and constructors fromsubclass

    FPT- APTECH 8/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    9/41

    #2 - Constructor Inheritance

    Describe Constructor Inheritance

    Constructor Chaining

    Rules for Constructors

    Explicitly invoke the base class constructor

    FPT- APTECH 9/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    10/41

    Constructor Inheritance

    In Java, cannot inherit constructors like inherit methods.

    The instance of the derived class will always first invoke theconstructor of the base class followed by the constructor of the

    derived class.

    Can explicitly invoke the base class constructor by using thesuper keyword in the derived class constructor declaration

    FPT- APTECH 10/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    11/41

    Constructor Chaining

    Parent

    F1

    F2

    FPT- APTECH 11/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    12/41

    Constructor Chaining

    The instance of the derived class will always first invoke theconstructor of the base class followed by the constructor of thederived class

    FPT- APTECH 12/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    13/41

    Rules for Constructors (MUST be clear and rem em ber)

    A default constructor will be automatically generated by thecompiler if no constructor is specified within the class.

    The default constructor is ALWAYS a no-arg constructor.

    If there is a constructor defined in the class, the defaultconstructor is no longer used.

    If you dont explicitly call a base class constructor in a derivedclass constructor, the compiler attempts to silently insert a callto the base classs default constructor before executing thecode in the derived class constructor

    FPT- APTECH 13/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    14/41

    Compiler-Generated Constructor Code

    FPT- APTECH 14/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    15/41

    Can yo u see any b ug ?

    FPT- APTECH 15/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    16/41

    How to fixed?

    FPT- APTECH 16/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    17/41

    Explicitly invoke the base class constructor

    FPT- APTECH 17/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    18/41

    #3 - Overriding Methods

    Method Signatures

    Define method overriding.

    FPT- APTECH 18/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    19/41

    Method Signatures

    Method has a signature comprises of:The number of parametersThe data types of parametersThe order in which the parameters are written.

    The return type of a method is not a part of its signature

    The signature of the method is written in parentheses next tothe method name.

    FPT- APTECH 19/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    20/41

    Overriding Methods

    Subclass define new method with the same signature as thesuperclass method.

    Overridden method cannot have a weaker access specifier thanthe access specifier of the method it overrides.

    FPT- APTECH 20/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    21/41

    Example of Overriding Methods

    FPT- APTECH 21/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    22/41

    super keyword

    Use to access superclasss members and constructors fromsubclass

    FPT- APTECH 22/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    23/41

    Example of Using super keyword

    FPT- APTECH 23/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    24/41

    #4 Overloading Methods

    Describe method overloading

    State and explain the this keyword.

    FPT- APTECH 24/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    25/41

    Overloading Methods

    Declaring more than one method with the same method namebut different signatures

    Constructor overloading

    Allows multiple ways of creating instances

    FPT- APTECH 25/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    26/41

    Example of Overloading Methods

    FPT- APTECH 26/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    27/41

    "this " keyword

    Refer to the current object of the class.

    Used to resolve conflicts

    between variables having samenames and to pass the currentobject as a parameter

    Cannot use the this keyword

    with static variables andmethods,

    FPT- APTECH 27/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    28/41

    #5 - Abstract Classes

    Define abstract classes

    Describe how to implement abstract classes

    Define abstract methods

    FPT- APTECH 28/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    29/41

    abstract Classes

    To serve as a framework that provides certain behavior for other classes.

    Contain zero or more abstract methods

    Cannot be instantiated.

    Can be inherited.

    The subclass must implement abstract methods declared in baseclass

    Otherwise it must be declared as abstract.

    Declare an abstract class by using the keyword abstract precede class keyword

    FPT- APTECH 29/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    30/41

    abstract Methods

    Method has only declaration and no implementationIs prefixed with the abstract keyword. The declaration does not contain any braces and is terminated by asemicolon.

    An abstract method is only a contract that the subclass will provideits implementation

    If a class includes abstract methods, the class itself must bedeclared abstract

    FPT- APTECH 30/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    31/41

    Example of abstract Classes and abstract Methods

    FPT- APTECH 31/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    32/41

    #6 - Final Variables, Methods, Classes

    Final Variables

    Final Methods

    Final Classes

    FPT- APTECH 32/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    33/41

    Final Variables

    final keyword is used with variables to indicate that they areconstant identifiers.

    Constant variables are assigned a value at the time of

    declaration and will not change anytime later.

    FPT- APTECH 33/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    34/41

    Final Methods

    To prevent a method from being overridden or hidden in a Javasubclass.

    If a change in implementation of method effects the consistentstate of the object

    Method that are declared private or part of the final class areimplicitly final.

    The final method cannot declared as abstract.

    FPT- APTECH 34/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    35/41

    Final Classes

    A class that cannot be subclassed.

    May or may not have final methods.

    Final classes can be instantiated

    FPT- APTECH 35/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    36/41

    #7 - Interfaces

    Introduction to Interfaces

    Implementing Multiple Interfaces

    FPT- APTECH 36/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    37/41

    What is an Interface?

    An interface is defined as a reference type

    Has only final variables, abstract methods signatures.Interface Methods Do Not Contain Method Bodies

    Cannot be instantiated.

    Can only be inherited by classes or other interfaces.

    A class that implements an interface is requi red to provide imp lementat ions for a l l the method s of the interface or elseshould be declared abstract.

    FPT- APTECH 37/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    38/41

    Example of Interface

    FPT- APTECH 38/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    39/41

    Implementing Multiple Interfaces

    An interface can extend one or more interfaces

    Multiple interfaces can be implemented in a single class.

    This implementation provides the functionality of multipleinheritance.

    Implement multiple interfaces by placing commas between theinterface names when implementing them in a class.

    A class must implement all inherited interface methods

    FPT- APTECH 39/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    40/41

    Example of Implementing Multiple Interfaces

    FPT- APTECH 40/41

  • 7/30/2019 7. JAVA InheritanceAndInterfaces

    41/41

    Thats about all for today!

    Inheritance

    Constructor Inheritance

    Overriding Methods

    Overloading of methods

    abstract class

    Using final keyword

    Interfaces

    Thank you al l for yo ur a t tent ion and pat ient !