Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Inheritance and
Polymorphism in
Java

MELJUN CORTES

Java Fundamentals and ObjectOriented Programming
The Complete Java Bo...
What You Should Learn
 Implementation

Inheritance







Extending a Class
Overriding a Method
Abstract Class
The ...
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 e...
Abstract Class
 You can mark a class to be purely for

code-sharing purposes:
abstract class Foo {
}
 An abstract class ...
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 ...
The protected Modifier
 …but will not be visible to any other class outside

the superclass’s package.

class Foo {
prote...
The super Keyword
 The super keyword is used to denote the

superclass:

class Bar extends Foo {
Bar() {
super();
}
int m...
The super Keyword
 Use super to access members of the

superclass:

…
int method() {
int temp = super.doSomething();
retu...
The super Keyword
 If you do not call a superclass constructor in your

subclass constructor, the compiler will add it fo...
The super Keyword
 If you do not call a superclass constructor in your

subclass constructor, the compiler will add it fo...
The super Keyword
 If the no-argument constructor does not exist in

the superclass, then the compiler will throw an
erro...
The super Keyword
 If you will explicitly call the superclass

constructor, it must be at the first line of your
subclass...
The super Keyword
 If you will explicitly call the superclass

constructor, it must be at the first line of your
subclass...
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 m...
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 ...
Using an Interface
 Best Practice: Method parameters and return

types should be interfaces and not concrete
classes when...
Using an Interface
 …that way, if you ever need to change your

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

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

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

...
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 getAllStud...
Creating an Interface
 All methods are public even if you don’t

specify it!
 You cannot create static methods.
 All fi...
Implementing an Interface
class StudendDaoOracleImpl

implements StudentDAO {
public List getAllStudents() {
final String ...
Implementing an Interface
 Use the implements keyword to declare

that a class inherits from an interface.
 You must imp...
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
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Java Lecture Inheritance Polymorphism

583 views

Published on

MELJUN CORTES Java Lecture Inheritance Polymorphism

Published in: Technology, Education
  • Be the first to comment

MELJUN CORTES Java Lecture Inheritance Polymorphism

  1. 1. Inheritance and Polymorphism in Java MELJUN CORTES Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp MELJUN CORTES
  2. 2. 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
  3. 3. Implementation Inheritance Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp
  4. 4. Extending a Class  Use extends keyword to create a subclass: class Bar extends class Foo { … }
  5. 5. Extending a Class  A subclass can still implement interfaces: class Bar extends Foo implements Pik, Pak, Boom { … }
  6. 6. 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… } }
  7. 7. Abstract Class  You can mark a class to be purely for code-sharing purposes: abstract class Foo { }  An abstract class cannot be instantiated.
  8. 8. The final Keyword  You can also mark a class so it cannot be subclassed: final class Bar { … }
  9. 9. The final Keyword  You can also mark a method so that method cannot be overridden: class Bar { final void method() { … } }
  10. 10. 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() { … } }
  11. 11. The protected Modifier  …but will not be visible to any other class outside the superclass’s package. class Foo { protected void method() { … } }
  12. 12. The super Keyword  The super keyword is used to denote the superclass: class Bar extends Foo { Bar() { super(); } int method() { return super.anotherMethod(); } }
  13. 13. The super Keyword  Use super to access members of the superclass: … int method() { int temp = super.doSomething(); return temp; } …
  14. 14. 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… } }
  15. 15. 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… } }
  16. 16. 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… } }
  17. 17. 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… } }
  18. 18. 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); } }
  19. 19. Interface Inheritance Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp
  20. 20. What is a Java interface?  Java construct for interface inheritance  Classes that implement an interface inherit only method signatures.  It’s a lot like a “contract”  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.
  21. 21. Using an Interface  Example:
  22. 22. Using an Interface  Example:
  23. 23. 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();
  24. 24. 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) {...
  25. 25. 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);
  26. 26. 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);
  27. 27. Creating an Interface <access modifier> interface <InterfaceName> { <constant declarations>* <method signatures>* }
  28. 28. 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); }
  29. 29. Creating an Interface  All methods are public even if you don’t specify it!  You cannot create static methods.  All fields are public, static and final even if you don’t specify it! (constants)
  30. 30. Implementing an Interface class StudendDaoOracleImpl implements StudentDAO { public List getAllStudents() { final String sql = “SELECT * FROM stu... ... } public Student getStudentWithId(int studentId) { ... } }
  31. 31. 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.
  32. 32. Interface Implementing an Interface  A class can implement more than one interface: class Foo implements Pik, Pak, Boom { … }
  33. 33. Extending an Interface  An interface can extend another interface using the extends keyword. interface Super extends Sub { … }
  34. 34. The End Java Fundamentals and Object-Oriented Programming The Complete Java Boot Camp

×