Sie sind auf Seite 1von 35

Inheritance and

Polymorphism in
Java

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
What You Should Learn
 Implementation  Interface Inheritance
Inheritance  What is a Java
 Extending a Class interface?
 Overriding a Method  Using Interfaces
 Abstract Class  Creating an Interface
 The final Keyword  Implementing an
 The protected Modifier Interface
 Extending an Interface
 The super Keyword

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Implementation
Inheritance

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Extending a Class
 Use extends keyword to create a
subclass:

class Bar extends Foo {



}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Extending a Class
 A subclass can still implement interfaces:
class Bar extends Foo implements Pik, Pak, Boom {

}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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…
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Abstract Class
 You can mark a class to be purely for
code-sharing purposes:

abstract class Foo {


}

 An abstract class cannot be instantiated.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Abstract Class
 Abstract classes may have both implemented
methods, or abstract methods (just method
signatures).
abstract class Foo {
public void doSomething() {
// implementation
}
public abstract void doYourOwnThing();
}
 Abstract methods must be implemented by the
subclass.
Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The final Keyword
 You can also mark a class so it cannot be
subclassed:
final class Bar {

}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The final Keyword
 You can also mark a method so that
method cannot be overridden:
class Bar {
final void method() {

}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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() {

}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The protected Modifier
 …but will not be visible to any other class outside
the superclass’s package.

class Foo {
protected void method() {

}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The super Keyword
 The super keyword is used to denote the
superclass:
class Bar extends Foo {
Bar() {
super();
}
int method() {
// do my own thing
return super.method();
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The super Keyword
 Use super to access members of the
superclass:

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

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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…
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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…
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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…
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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…
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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);
}
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Interface
Inheritance

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Using an Interface
 Example:

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Using an Interface
 Example:

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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();

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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) {...

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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);

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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);

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Creating an Interface

<access modifier> interface <InterfaceName> {


<constant declarations>*
<method signatures>*
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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);
}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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)

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Implementing an Interface
class StudendDaoOracleImpl
implements StudentDAO {

public List getAllStudents() {


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

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

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
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.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Interface Implementing an
Interface
 A class can implement more than one
interface:

class Foo implements Pik, Pak, Boom {



}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Extending an Interface
 An interface can extend another interface
using the extends keyword.

interface Super extends Sub {



}

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
The End

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved

Das könnte Ihnen auch gefallen