Вы находитесь на странице: 1из 34

Inheritance and Polymorphism in Java

Java Fundamentals and ObjectOriented Programming

MELJUN CORTES

MELJUN CORTES

The Complete Java Boot Camp

What You Should Learn


Implementation

Inheritance

Extending a Class Overriding a Method Abstract Class The final Keyword The protected Modifier The super Keyword

Interface Inheritance What is a Java interface? Using Interfaces Creating an Interface Implementing an Interface Extending an Interface

Implementation Inheritance
Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp

Extending a Class
Use extends keyword to create a

subclass:
class Bar extends class Foo { }

Extending a Class
A subclass can still implement interfaces:
class Bar extends Foo implements Pik, Pak, Boom { }

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 } }

Abstract Class
You can mark a class to be purely for

code-sharing purposes:
abstract class Foo { }
An abstract class cannot be instantiated.

The final Keyword


You can also mark a class so it cannot be

subclassed:
final class Bar { }

The final Keyword


You can also mark a method so that

method cannot be overridden:


class Bar { final void method() { } }

The protected Modifier


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() { } }

The protected Modifier


but will not be visible to any other class outside

the superclasss package.

class Foo { protected void method() { } }

The super Keyword


The super keyword is used to denote the

superclass:
class Bar extends Foo { Bar() { super(); } int method() { return super.anotherMethod(); } }

The super Keyword


Use super to access members of the

superclass:
int method() { int temp = super.doSomething(); return temp; }

The super Keyword


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 } }

The super Keyword


If you do not call a superclass constructor in your

subclass constructor, the compiler will add it for you:


after compilation: class Bar extends Foo { Bar() { super(); added by compiler do stuff } }

The super Keyword


If the no-argument constructor does not exist in

the superclass, then the compiler will throw an error:


after compilation: class Bar extends Foo { Bar() { super(); added by compiler do stuff } }

The super Keyword


If you will explicitly call the superclass

constructor, it must be at the first line of your subclass constructor


class Bar extends Foo { Bar(int x, int y) { super(x, y); do stuff } }

The super Keyword


If you will explicitly call the superclass

constructor, it must be at the first line of your subclass constructor


class Bar extends Foo { Bar(int x, int y) { do stuff super(x, y); } }

Interface Inheritance
Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp

What is a Java interface?


Java construct for interface inheritance

Classes that implement an interface inherit only method signatures. Any class that implements a particular interface must implement all its methods. Useful in large-team environments. Compels other programmers in the team to implement their classes in a way that will fit with your own work.

Its a lot like a contract

Using an Interface
Example:

Using an Interface
Example:

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();

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) {...

Using an Interface
that way, if you ever need to change your

implementation, you only need to edit one line of code.


...// in the getAllStudents method: List students = new ArrayList(); ... return students; ...// somewhere else in the application final List allStudents = dao.getAllStudents(); allStudents.add(joey); service.enrollStuents(allStudents);

Using an Interface
that way, if you ever need to change your

implementation, you only need to edit one line of code.


...// in the getAllStudents method: List students = new LinkedList(); ... return students; ...// somewhere else in the application final List allStudents = dao.getAllStudents(); allStudents.add(joey); service.enrollStuents(allStudents);

Creating an Interface

<access modifier> interface <InterfaceName> { <constant declarations>* <method signatures>* }

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); }

Creating an Interface
All methods are public even if you dont

specify it!
You cannot create static methods. All fields are public, static and final even if

you dont specify it! (constants)

Implementing an Interface
class StudendDaoOracleImpl

implements StudentDAO { public List getAllStudents() {


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

public Student
getStudentWithId(int studentId) { ... } }

Implementing an Interface
Use the implements keyword to declare

that a class inherits from an interface.


You must implement all methods or

declare your class abstract.

Interface Implementing an Interface


A class can implement more than one

interface:
class Foo implements Pik, Pak, Boom { }

Extending an Interface
An interface can extend another interface

using the extends keyword.


interface Super extends Sub { }

The End
Java Fundamentals and Object-Oriented Programming The Complete Java Boot Camp