34
Inheritance and Inheritance and Polymorphism in Polymorphism in Java Java Java Fundamentals and Object- Java Fundamentals and Object- Oriented Programming Oriented Programming MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCS MBA,MPA,BSCS

MELJUN CORTES Java inheritance and polymorphism in java

Embed Size (px)

Citation preview

Inheritance and Inheritance and Polymorphism in Polymorphism in JavaJava

Java Fundamentals and Object-Java Fundamentals and Object-Oriented ProgrammingOriented Programming

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

What You Should LearnWhat You Should Learn

Implementation Implementation InheritanceInheritance Extending a ClassExtending a Class Overriding a MethodOverriding a Method Abstract ClassAbstract Class The The finalfinal Keyword Keyword The The protectedprotected Modifier Modifier The The supersuper Keyword Keyword

Interface InheritanceInterface Inheritance What is a Java What is a Java

interface?interface? Using InterfacesUsing Interfaces Creating an InterfaceCreating an Interface Implementing an Implementing an

InterfaceInterface Extending an InterfaceExtending an Interface

Implementation Implementation InheritanceInheritance

Java Fundamentals and Object-Java Fundamentals and Object-Oriented ProgrammingOriented Programming

The Complete Java Boot CampThe Complete Java Boot Camp

Extending a ClassExtending a Class

Use Use extendsextends keyword to create a keyword to create a subclass:subclass:

class Bar extends class Foo {

}

Extending a ClassExtending a Class

A subclass can still implement interfaces:A subclass can still implement interfaces:

class Bar extends Foo implements Pik, Pak, Boom {class Bar extends Foo implements Pik, Pak, Boom {

……

}}

Overriding a methodOverriding a method

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

void method() {void method() {……do something…do something…

}}}}class Bar extends Foo {class Bar extends Foo {

void method() {void method() {……do something else…do something else…

}}}}

Abstract ClassAbstract Class

You can mark a class to be purely for You can mark a class to be purely for code-sharing purposes:code-sharing purposes:

abstract class Foo {abstract class Foo {

}}

An abstract class cannot be instantiated.An abstract class cannot be instantiated.

The The final final KeywordKeyword

You can also mark a class so it cannot be You can also mark a class so it cannot be subclassed:subclassed:

final class Bar {final class Bar {

……

}}

The The finalfinal Keyword Keyword

You can also mark a method so that You can also mark a method so that method cannot be overridden:method cannot be overridden:

class Bar {class Bar {

final void method() {final void method() {

……

}}

}}

The The protectedprotected Modifier Modifier

Methods and attributes that are marked Methods and attributes that are marked protectedprotected will be visible to a subclass even if it is in another will be visible to a subclass even if it is in another package…package…

class Foo {class Foo {

protected void method() {protected void method() {

……

}}

}}

The The protectedprotected Modifier Modifier

……but will not be visible to any other class outside but will not be visible to any other class outside the superclass’s package.the superclass’s package.

class Foo {class Foo {protected void method() {protected void method() {

……}}

}}

The The supersuper Keyword Keyword

The The supersuper keyword is used to denote the keyword is used to denote the superclass:superclass:class Bar extends Foo {class Bar extends Foo {

Bar() {Bar() {super();super();

}}int method() {int method() {

return super.anotherMethod();return super.anotherMethod();}}

}}

The The supersuper Keyword Keyword

Use Use supersuper to access members of the to access members of the superclass:superclass:……int method() {int method() {

int temp = super.doSomething();int temp = super.doSomething();return temp;return temp;

}}……

The The supersuper Keyword Keyword

If you do not call a superclass constructor in your If you do not call a superclass constructor in your subclass constructor, the compiler will add it for subclass constructor, the compiler will add it for you:you:your code:your code:

class Bar extends Foo {class Bar extends Foo {

Bar() {Bar() {

……do stuff…do stuff…

}}

}}

The The supersuper Keyword Keyword

If you do not call a superclass constructor in your If you do not call a superclass constructor in your subclass constructor, the compiler will add it for subclass constructor, the compiler will add it for you:you:after compilation:after compilation:class Bar extends Foo {class Bar extends Foo {

Bar() {Bar() {super();super(); added by compiler added by compiler……do stuff…do stuff…

}}}}

The The supersuper Keyword Keyword

If the no-argument constructor does not exist in If the no-argument constructor does not exist in the superclass, then the compiler will throw an the superclass, then the compiler will throw an error:error:after compilation:after compilation:class Bar extends Foo {class Bar extends Foo {

Bar() {Bar() {super();super(); added by compiler added by compiler……do stuff…do stuff…

}}}}

The The supersuper Keyword Keyword

If you will explicitly call the superclass If you will explicitly call the superclass constructor, it must be at the first line of your constructor, it must be at the first line of your subclass constructorsubclass constructor class Bar extends Foo {class Bar extends Foo {

Bar(int x, int y) {Bar(int x, int y) {super(x, y);super(x, y);……do stuff…do stuff…

}}}}

The The supersuper Keyword Keyword

If you will explicitly call the superclass If you will explicitly call the superclass constructor, it must be at the first line of your constructor, it must be at the first line of your subclass constructorsubclass constructor class Bar extends Foo {class Bar extends Foo {

Bar(int x, int y) {Bar(int x, int y) {……do stuff…do stuff…super(x, y);super(x, y);

}}}}

Interface Interface InheritanceInheritance

Java Fundamentals and Object-Java Fundamentals and Object-Oriented ProgrammingOriented Programming

The Complete Java Boot CampThe Complete Java Boot Camp

What is a Java interface?What is a Java interface?

Java construct for Java construct for interface inheritanceinterface inheritance Classes that Classes that implementimplement an interface inherit an interface inherit

only method signatures.only method signatures.

It’s a lot like a “It’s a lot like a “contractcontract”” Any class that Any class that implementsimplements a particular a particular

interface interface mustmust implement implement allall its methods. its methods. Useful in large-team environments. Compels Useful in large-team environments. Compels

other programmers in the team to implement other programmers in the team to implement their classes in a way that will fit with your own their classes in a way that will fit with your own work. work.

Using an InterfaceUsing an Interface

Example:Example:

Using an InterfaceUsing an Interface

Example:Example:

Using an InterfaceUsing an Interface

Best Practice: If an interface exists, never Best Practice: If an interface exists, never refer to the concrete class unless you refer to the concrete class unless you absolutely have to. Refer to the interface absolutely have to. Refer to the interface instead:instead:

List listOfStudents = new ArrayList();

Using an InterfaceUsing an Interface

Best Practice: Method parameters and return Best Practice: Method parameters and return types should be interfaces and not concrete types should be interfaces and not concrete classes whenever possible.classes whenever possible.

List getAllStudents() {

final List students = new ArrayList();

...

return students;

}

void enrollStudents(final List students) {...

Using an InterfaceUsing an Interface

……that way, if you ever need to change your that way, if you ever need to change your implementation, you only need to edit one line of implementation, you only need to edit one line of code.code.

...// in the getAllStudents method:List students = new ArrayList();...return students;

...// somewhere else in the applicationfinal List allStudents = dao.getAllStudents();allStudents.add(joey);service.enrollStuents(allStudents);

Using an InterfaceUsing an Interface

……that way, if you ever need to change your that way, if you ever need to change your implementation, you only need to edit one line of implementation, you only need to edit one line of code.code.

...// in the getAllStudents method:List students = new LinkedList();...return students;

...// somewhere else in the applicationfinal List allStudents = dao.getAllStudents();allStudents.add(joey);service.enrollStuents(allStudents);

Creating an InterfaceCreating an Interface

<access modifier> interface <InterfaceName> {

<constant declarations>*

<method signatures>*

}

Creating an InterfaceCreating 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 InterfaceCreating an Interface

All methods are All methods are publicpublic even if you don’t even if you don’t specify it!specify it!

You cannot create static methods.You cannot create static methods.

All fields are All fields are publicpublic, , staticstatic and and finalfinal even if even if you don’t specify it! (constants)you don’t specify it! (constants)

Implementing an InterfaceImplementing an Interface

class StudendDaoOracleImpl

implements StudentDAO {

public List getAllStudents() { final String sql = “SELECT * FROM stu... ... }

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

Implementing an InterfaceImplementing an Interface

Use the Use the implementsimplements keyword to declare keyword to declare that a class inherits from an interface.that a class inherits from an interface.

You must implement all methods or You must implement all methods or declare your class abstract.declare your class abstract.

Interface Implementing an Interface Implementing an InterfaceInterface A class can implement more than one A class can implement more than one

interface:interface:

class Foo implements Pik, Pak, Boom {class Foo implements Pik, Pak, Boom {

……

}}

Extending an InterfaceExtending an Interface

An interface can extend another interface An interface can extend another interface using the extends keyword.using the extends keyword.

interface Super interface Super extendsextends Sub { Sub {

……

}}

The EndThe End

Java Fundamentals and Object-Oriented Java Fundamentals and Object-Oriented ProgrammingProgramming