MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

download MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

of 34

Transcript of MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    1/34

    Inheritance andPolymorphism in

    Java

    Java Fundamentals and Object-

    Oriented Programming

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    2/34

    What You Should Learn

    Implementation

    Inheritance

    Extending a Class

    Overriding a Method Abstract Class

    The finalKeyword

    TheprotectedModifier

    The superKeyword

    Interface Inheritance

    What is a Java

    interface?

    Using Interfaces

    Creating an Interface

    Implementing an

    Interface

    Extending an Interface

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    3/34

    Implementation

    Inheritance

    Java Fundamentals and Object-Oriented Programming

    The Complete Java Boot Camp

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    4/34

    Extending a Class

    Use extendskeyword to create a

    subclass:

    class Bar extends class Foo {

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    5/34

    Extending a Class

    A subclass can still implement interfaces:

    class Bar extends Foo implements Pik, Pak, Boom {

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    6/34

    Overriding a method

    You can re-implement an inherited method:class Foo {

    void method() {

    do something

    }}

    class Bar extends Foo {

    void method() {

    do something else

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    7/34

    Abstract Class

    You can mark a class to be purely for

    code-sharing purposes:

    abstract class Foo {

    }

    An abstract class cannot be instantiated.

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    8/34

    The final Keyword

    You can also mark a class so it cannot be

    subclassed:

    final class Bar {

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    9/34

    The finalKeyword

    You can also mark a method so that

    method cannot be overridden:

    class Bar {

    final void method() {

    }

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    10/34

    TheprotectedModifier

    Methods and attributes that are marked protected

    will be visible to a subclass even if it is in another

    package

    class Foo {

    protected void method() {

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    11/34

    TheprotectedModifier

    but will not be visible to any other class outsidethe superclasss package.

    class Foo {

    protected void method() {

    }

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    12/34

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    13/34

    The superKeyword

    Use superto access members of thesuperclass:

    int method() {int temp = super.doSomething();

    return temp;

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    14/34

    The superKeyword

    If you do not call a superclass constructor in your

    subclass constructor, the compiler will add it for

    you:

    your code:class Bar extends Foo {

    Bar() {

    do stuff

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    15/34

    The superKeyword

    If you do not call a superclass constructor in yoursubclass constructor, the compiler will add it foryou:

    after compilation:

    class Bar extends Foo {Bar() {

    super(); added by compiler

    do stuff

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    16/34

    The superKeyword

    If the no-argument constructor does not exist inthe superclass, then the compiler will throw anerror:

    after compilation:

    class Bar extends Foo {

    Bar() {

    super(); added by compiler

    do stuff

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    17/34

    The superKeyword

    If you will explicitly call the superclassconstructor, it must be at the first line of yoursubclass constructor

    class Bar extends Foo {

    Bar(int x, int y) {

    super(x, y);

    do stuff

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    18/34

    The superKeyword

    If you will explicitly call the superclassconstructor, it must be at the first line of yoursubclass constructor

    class Bar extends Foo {

    Bar(int x, int y) {

    do stuff

    super(x, y);

    }}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    19/34

    Interface

    Inheritance

    Java Fundamentals and Object-Oriented Programming

    The Complete Java Boot Camp

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    20/34

    What is a Java interface?

    Java construct for interface inheri tance

    Classes that implementan interface inherit

    only method signatures.

    Its a lot like a contractAny class that implementsa particular

    interface mustimplement allits methods.

    Useful in large-team environments. Compelsother programmers in the team to implement

    their classes in a way that will fit with your own

    work.

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    21/34

    Using an Interface

    Example:

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    22/34

    Using an Interface

    Example:

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    23/34

    Using an Interface

    Best Practice: If an interface exists, never

    refer to the concrete class unless you

    absolutely have to. Refer to the interface

    instead:

    List listOfStudents = new ArrayList();

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    24/34

    Using an Interface

    Best Practice: Method parameters and return

    types should be interfaces and not concrete

    classes whenever possible.

    List getAllStudents() {

    final List students = new ArrayList();

    ...

    return students;

    }

    void enrollStudents(final List students) {...

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    25/34

    Using an Interface

    that way, if you ever need to change yourimplementation, you only need to edit one line ofcode.

    ...// in the getAllStudents method:

    List students = new rray ist();

    ...

    return students;

    ...// somewhere else in the application

    final List allStudents = dao.getAllStudents();

    allStudents.add(joey);

    service.enrollStuents(allStudents);

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    26/34

    Using an Interface

    that way, if you ever need to change yourimplementation, you only need to edit one line ofcode.

    ...// in the getAllStudents method:List students = new inked ist();

    ...

    return students;

    ...// somewhere else in the application

    final List allStudents = dao.getAllStudents();

    allStudents.add(joey);

    service.enrollStuents(allStudents);

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    27/34

    Creating an Interface

    interface {

    *

    *}

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    28/34

    Creating an Interface

    public interface StudentDAO {

    int UNDERGRAD = 1;

    int MASTERAL = 2;

    int DOCTORAL = 3;

    List getAllStudents();

    Student getStudentWithId(int studentId);

    void saveStudent(Student student);

    void deleteStudent(Student student);

    void deleteStudentWithId(int studentId);

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    29/34

    Creating an Interface

    All methods are publiceven if you dont

    specify it!

    You cannot create static methods.

    All fields are public, staticand finaleven if

    you dont specify it! (constants)

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    30/34

    Implementing an Interface

    class StudendDaoOracleImpl

    implementsStudentDAO {

    publicList getAllStudents() {

    final String sql = SELECT * FROM stu......

    }

    publicStudent

    getStudentWithId(int studentId) {

    ...

    }

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    31/34

    Implementing an Interface

    Use the implementskeyword to declare

    that a class inherits from an interface.

    You must implement all methods ordeclare your class abstract.

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    32/34

    Interface Implementing an

    Interface

    A class can implement more than one

    interface:

    class Foo implements Pik, Pak, Boom {

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    33/34

    Extending an Interface

    An interface can extend another interface

    using the extends keyword.

    interface Super extendsSub {

    }

  • 8/10/2019 MELJUN CORTES JAVA InheritanceAndPolymorphismInJava

    34/34

    The End

    Java Fundamentals and Object-Oriented

    Programming