pemrograman lanjut - universitas brawijayaafif.lecture.ub.ac.id/files/2014/04/inheritance.pdf ·...

42
Pemrograman Lanjut PTIIK - 2014 Inheritance

Upload: truongtu

Post on 27-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Pemrograman Lanjut

PTIIK - 2014

Inheritance

Page 2: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Objectives

How inheritance promotes software reusability.

The notions of superclasses and subclasses

To use keyword extends to create a class that

inherits attributes and behaviors from another

class.

To use access modifier protected to give

subclass methods access to superclass

members.

To access superclass members with super.

Page 3: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Introduction - Inheritance

Object oriented languages have a feature called

inheritance.

Inheritance enables you to define a new class

based upon an existing class.

The new class is similar to the existing class, but

has additional member variables and methods.

This makes programming easier because you

can build upon an existing class instead of

starting out from scratch.

Page 4: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Introduction - Inheritance

Software reusability

Create new class from existing class

Absorb existing class’s data and behaviors

Enhance with new capabilities

New class extends Existing class

New class

• More specialized group of objects

• Behaviors inherited from Existing class

– Can customize

• Additional behaviors

Page 5: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Problems with Hacking Code

If you have the source code for a class, you

could copy the code and change it to do what

you wanted. Before object oriented programming

that was what was done. But there are at least

two problems with this:

It is hard to stay organized.

You need to study the original code.

The automatic inheritance mechanism of Java

greatly relieves both of these problems.

Page 6: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Problems with Hacking Code

In diagrams that show inheritance, an

arrow points from the new class to the

class it is based upon. The arrow is

sometimes labeled "is a".

In these notes, clouds represent classes

and rectangles represent objects.

Page 7: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Single Inheritance

The class that is used to define a new class is called a

parent class (or superclass or base class.) The class

based on the parent class is called a child class (or

subclass or derived class.)

In Java, (unlike with humans) children inherit

characteristics from just one parent. This is called single

inheritance. Some languages allow a child to inherit

from more than one parent. This is called multiple

inheritance. With multiple inheritance, it is sometimes

hard to tell which parent contributed what characteristics

to the child (as with humans). Java avoids these

problems by using single inheritance.

Page 8: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Interchangeable Phrases

There are three sets of phrases for describing

inheritance relationships: parent/child,

superclass/subclass , base class/derived class.

Programmers use all three sets interchangeably.

Page 9: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Is-a Relationship

The picture shows a parent class and a child

class. The line between them shows the "is-a"

relationship. The arrow points to the parent class

from the child class. The picture can be read as

"a Ford is-a automobile."

Page 10: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Inheritance is between classes, not between

objects.

A parent class is a blueprint that is followed

when an object is constructed. A child class of

the parent is another blueprint (that looks much

like the original), but with added features. The

child class is used to construct objects that look

like the parent's objects, but with added features.

Page 11: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Objects

The picture shows a parent class and a child class, and

some objects that have been constructed from each.

These objects are shown as rectangles (to convey the

idea that they are more real than the classes, which are

only designs).

Page 12: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Hierarchies

This picture shows a hierarchy of classes. It shows that

"Ford is-a automobile," "Nissan is-a automobile," and that

"VW is-a automobile." It also shows that "Sentra is-a

Nissan."

Page 13: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Superclasses and subclasses

There are two kinds of superclass base on class

hierarchy, i.e. direct superclass and indirect superclass.

The direct superclass is the superclass from which

the subclass explicitly inherits.

An indirect superclass is any class above the direct

superclass in the class hierarchy, which defines the

inheritance relationships between classes.

Page 14: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Characteristics of Inheritance

New class inherits the members of an existing class

Existing class is called the superclass, and the new class is

the subclass.

Subclass normally adds its own fields and methods that are

specific to the subclass.

Subclass is more specific than its superclass and represents a

more specialized group of objects.

Java does not support multiple inheritance (which occurs when

a class is derived from more than one direct superclass).

Unlike properties and methods, the constructors of a

superclass are not inherited in the subclass

Page 15: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Other Example

Superclass Subclasses

Student GraduateStudent, UndergraduateStudent

Shape Circle, Triangle, Rectangle

Loan CarLoan, HomeImprovementLoan, MortgageLoan

Employee Faculty, Staff

BankAccount CheckingAccount, SavingsAccount

Page 16: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Inheritance hierarchy for university

CommunityMembers

Page 17: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Inheritance hierarchy for Shapes

Page 18: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

18

Inheritance hierarchy 2D-Shapes Class

Page 19: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Relative Terms

The syntax for deriving a child class from a

parent class is:

class childClass extends parentClass

{

// new characteristics of the child class go here

}

Page 20: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Video Store Example

Page 21: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Using Inheritance

The class Movie is a subclass of Video.

Page 22: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

22

• The keyword super refers to the superclass of the class in

which super appears.

• It can be used to call a superclass constructor and a

superclass method.

• The syntax to call a superclass constructor:

super() or super(parameters);

• The statement super() invokes the no-arg constructor of its

superclass, and the statement super(arguments) invokes

the superclass constructor that matches the arguments.

• The statement super() or super(arguments) must appear in

the first line of the subclass constructor.

Using the super Keyword

Page 23: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Using a Super Class's Constructor

The class definition for Video has a constructor that

initializes the member data of Video objects. The class

Movie has a constructor that initializes the data of Movie

objects. The constructor for class Movie looks like this: // constructor

public Movie( String ttl, int lngth, String dir, String

rtng ) {

super(ttl, lngth); // use the super class's constuctor

director = dir;

rating = rtng; // initialize the members new to Movie

}

Note: super() must be the first statement in the subclass's constructor.

Page 24: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

The Constructor Invoked by super()

A constructor for a child class always starts with

an invocation of one of the constuctors in the

parent class. If the parent class has several

constructors then the one which is invoked is

determined by matching argument lists.

Page 25: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

No-argument Constructor

It looks like there is no need to invoke the parent class's

constructor since all variables are initialized in this one.

However a constructor from the parent class is always

invoked even if you don't explicity ask for it.

Page 26: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

The Java compiler regards the above code as

"shorthand" for this:

As always, super() comes first, even if you don't write it

in. If the parent does not have a no-argument constructor,

then using this "shorthand" causes a syntax error.

Page 27: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Instantiating Movie

In the example program, the class definition for Video includes a

constructor, so the default constructor was not automatically

supplied. So the constructor proposed for Movie causes a syntax

error. Let us not use it.

Page 28: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

The statement item2.show() calls the show() method of

item2. This method was inherited without change from

the class Video. This is what it looks like:

public void show() {

System.out.println( title + ", " + length + " min.

available:" + avail );

}

It does not mention the new variables that have been

added to objects of type Movie, so nothing new is printed

out.

Page 29: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Overriding Methods

We need a new show() method in the class Movie:

// added to class Movie

public void show() {

System.out.println( title + ", " + length + "

min. available:" + avail );

System.out.println( "dir: " + director + " " +

rating );

}

Now, even though the parent has a show() method the

new definition of show() in the child class will override

the parent's version.

Page 30: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Overriding Methods

A child's method overrides a parent's method when

it has the same signature as a parent method.

Now the parent has its method, and the child has its

own method with the same signature.

(Remember that the signature of a method is the

name of the method and its parameter list.)

An object of the parent type includes the method

given in the parent's definition.

An object of the child type includes the method given

in the child's definition.

Page 31: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Using super in a Child's Method

Sometimes (as in the example) you want a child class to have its

own method, but that method includes everything the parent's

method does.

Page 32: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

The Object Class

Remember the rule: every constructor starts out with a

super() constructor. If you don't explicitly put it in, then

the Java compiler automatically puts it in for you.

Page 33: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

The Object Class

All classes have a parent class (a super class)

except one. The class at the top of the Java

class hierarchy is called Object. If a class

definition does not specify a parent class then it

automatically has Object as a parent class. The

compiler automatically assumes, for example:

Page 34: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Only One Class Hierarchy

If you define a class that does not explicitly

extend another class, then it automatically

extends Object.

If you define the class so that it extends a

class other than Object, then that class

either extends another class or extends

Object. Now that class in turn must either

extend another class or extend Object.

There is no way to end the chain except by

(ultimately) extending Object.

You might cleverly try to have class A

extend class B, and have class B extend

class A. But this (and other loops) is not

allowed.

Page 35: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

protected Members

protected access

Intermediate level of protection between public and

private

protected members accessible by

• superclass members

• subclass members

• Class members in the same package

Subclass access to superclass member

• Keyword super and a dot (.)

Page 36: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Note 1

Methods of a subclass cannot directly access

private members of their superclass.

A subclass can change the state of private

superclass instance variables only through non-

private methods provided in the superclass and

inherited by the subclass (encapsulation).

Page 37: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Note 2

Declaring private instance variables helps

programmers test, debug and correctly modify

systems.

If a subclass could access its superclass’s

private instance variables, classes that inherit

from that subclass could access the instance

variables as well.

This would propagate access to what should be

private instance variables, and the benefits of

information hiding would be lost.

Page 38: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Note 3

The Java compiler sets the superclass of a class

to Object when the class declaration does not

explicitly extend a superclass

Page 39: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Common Error 1

It is a syntax error to override a method with a

more restricted access modifier :

a public method of the superclass cannot become a

protected or private method in the subclass;

a protected method of the superclass cannot become

a private method in the subclass.

Doing so would break the “is-a” relationship in

which it is required that all subclass objects be

able to respond to method calls that are made to

public methods declared in the superclass.

Page 40: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Common Error 2

If a public method could be overridden as a

protected or private method, the subclass

objects would not be able to respond to the same

method calls as superclass objects.

Once a method is declared public in a

superclass, the method remains public for all

that class’s direct and indirect subclasses.

Page 41: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

Tugas

Buatlah class sesuai dengan hirarki class di bawah ini

Buat: 2 atribut, 1 method, dan 2 konstruktor pada class

Binatang

Tambahkan pada masing2 class turunan Binatang: 2

atribut, 1 method, 1 konstruktor, dan 1 overriding method

Binatang

Kucing Ayam Sapi

Page 42: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/04/Inheritance.pdf · Absorb existing class’s data and behaviors Enhance with new capabilities New class

[email protected]

081 331 834 734 / 088 160 127 40