03 inheritance, polymorphism and interface

14
INHERITANCE, POLYMORPHISM AND INTERFACE Object Oriented Programming

Upload: danita-ureta-balasta

Post on 30-Sep-2015

254 views

Category:

Documents


6 download

DESCRIPTION

Object Oriented Programming

TRANSCRIPT

  • INHERITANCE, POLYMORPHISM AND INTERFACE Object Oriented Programming

    Advanced Object-Oriented Concepts (Part 1)

  • InheritanceThe class that is used to define a new class is called a parent class (or superclass or base class). A class created to inherit from a base class is called a child class (or subclass or derived class) .

    Inheritance, Polymorphism and Interface*

    Advanced Object-Oriented Concepts (Part 1)

  • InheritanceBenefits of Inheritance in OOP:ReusabilityOnce a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses. Therefore, you can encode a method only once and they can be used by all subclasses. A subclass only needs to implement its differences with its parent.

    Inheritance, Polymorphism and Interface*

    Advanced Object-Oriented Concepts (Part 1)

  • InheritanceThe extends KeywordThis is used to set the relationship between a child class and a parent class.

    class childClass extends parentClass{ //characteristics of the child here}

    Inheritance, Polymorphism and Interface*

    Advanced Object-Oriented Concepts (Part 1)

  • InheritanceUsing the super KeywordIf your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keywordsuperThings to consider when using the super constructor call: The super() call must occur as the first statement in a constructor.The super() call can only be used in a constructor definition.These imply that the this() construct and the super() calls cannot both occur in the same constructor.

    Inheritance, Polymorphism and Interface*

    Advanced Object-Oriented Concepts (Part 1)

  • Method OverridingOverriding is the process of superseding a superclass method by defining a method in the subclass that has the same name and parameters as a method in the superclass. It modifies the implementation of a particular piece of behavior for a subclass.

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Method OverridingRules apply to overridden methods:An overriding method cannot be less accessible than the method it overrides.An overriding method cannot throw more exceptions than the method it overrides.A static method cannot be overridden to be non-static.

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Final Classes and MethodsYou use thefinalkeyword in a method declaration to indicate that the method cannot be overridden by subclassesA class that is declared final cannot be subclassedpublic final class ClassName {}

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Abstract Classes and MethodsAn abstract class is a class that cannot be instantiated.It is declared abstract it may or may not include abstract methods.

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Abstract Classes and MethodsAn abstract method is a method that is declared without an implementation (no curly braces and no method statements). To create an abstract method, write the method declaration without the body and use the abstract keyword.

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Abstract Classes and MethodsCoding Guideline:Use abstract classes to define broad types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • Interfacescollection of constants and method declarations method declarations do not include an implementation (there is no method body)

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • InterfacesReasons for using interfaces:To have unrelated classes implement similar methodsTo reveal an objects programming interface without revealing its classTo model multiple inheritance which allows a class to have more than one superclass

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

  • InterfacesTo use an interface, you include the keyword implements and the interface name in the class header.The syntax for implementing interfaces is:

    public class ClassName implements interfaceName {//code here}

    Advanced Object-Oriented Concepts (Part 1)*

    Advanced Object-Oriented Concepts (Part 1)

    *