Anda di halaman 1dari 0

F0031

Inheritance
The class that is used to define a new class is
called a parent class (or superclass or base
class).
A class created to inherit from a base class is
called a child class (or subclass or derived
class) .

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 1 of 25

F0031

Inheritance
Sets of phrases to describe the inheritance
relationship

Parent

Child

Superclass

Subclass

Inheritance, Polymorphism, and Interfaces

Base Class

Derived Class

* Property of STI
Page 2 of 25

1 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
2 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Inheritance
Object class hierarchy
Object

Class A

Class B

Class D

Class C

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 3 of 25

F0031

Inheritance
Benefits of Inheritance in OOP: Reusability

Once a behavior (method) is defined in a


superclass, that behavior is automatically
inherited by all subclasses.

Therefore, you can encode a method only once


and they can be used by all subclasses.

A subclass only needs to implement its


differences with its parent.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 4 of 25

3 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
4 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

The extends Keyword

This is used to set the relationship between a


child class and a parent class.
To derive a class, we write:
class childClass extends parentClass
{
//characteristics of the child
here
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 5 of 25

F0031

The extends Keyword


Example 1:
class Person {
protected String name;
protected String address;
public Person() {
System.out.println(Person
Information);
name = ;
address = ;
}

}
class Student extends Person
{
public Student() {
System.out.println(Student
Information);
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 6 of 25

5 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
6 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

The extends Keyword


Example 2:
class GraduateStud extends Student
{
public void courseGrade() {
int total = 0;
for(int i = 0; i < num_of_tests; i++) {
total += test[i];
}
if(total/num_of_tests >= 80) {
courseGrade = Pass;
} else {
courseGrade = No Pass;
}
}
}
class UnderGraduateStud extends Student
{
public void courseGrade() {
int total = 0;
for(int i = 0; i < num_of_tests; i++) {
total += test[i];
}
if(total/num_of_tests >= 70) {
courseGrade = Pass;
} else {
ourseGrade = No Pass;
}
}
}
Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 7 of 25

F0031

Using the super Keyword

Example 1:
class Student extends Person
{
public Student() {
super(SomeName,
SomeAddress);
System.out.println(Student
Information);
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 8 of 25

7 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
8 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Using the super Keyword

Things to consider when using the super


constructor call:

The super() call must occur as the first


statement in a constructor.

The super() call can only be used in a


constructor definition.

These imply that the this() construct and the


super() calls cannot both occur in the same
constructor.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 9 of 25

F0031

Using the super Keyword

Example 2:
class Student extends Person
{
public Student() {
super.name = somename;
super.address = someaddress;
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 10 of 25

9 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
10 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Method Overriding
Overriding is the process of superseding a
superclass method by defining a method in the
subclass that has the same name and
parameters as a method in the superclass.

It modifies the implementation of a particular


piece of behavior for a subclass.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 11 of 25

F0031

Method Overriding
Example 1:
class Person1
{
void laugh()
{
System.out.print(\nhahahaha);
}
}
class Person2 extends Person1
{
void laugh()
{
System.out.print(\nhohohoho);
}
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 12 of 25

11 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
12 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Method Overriding
Example 2:
public class Person
{

public String getName() {


System.out.println(Parent:
getName);
return name;
}
}
public class Student extends Person
{

public String getName() {


System.out.println(Student:
getName);
return name;
}
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 13 of 25

F0031

Method Overriding
Rules apply to overridden methods:

An overriding method cannot be less accessible


than the method it overrides.

An overriding method cannot throw more


exceptions than the method it overrides.

A static method cannot be overridden to be


non-static.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 14 of 25

13 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
14 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Writing Final Classes and


Methods
To declare a final class, we include the final
keyword as modifier for the class as follows:
public final class ClassName {

}
Example:
public final class Person {

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 15 of 25

F0031

Writing Final Classes and


Methods
To declare final methods, include the final
keyword as modifier in the method declaration
as follows:
public final [returnType]
[methodName]([parameters])
{

}
Example:
public final String getName() {
return name;
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 16 of 25

15 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
16 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Polymorphism
In Java, we can create a reference that is of type superclass
to an object of its subclass.
Example:
public static main(String[] args) {
Student studObject = new Student();
Employee empObject = new Employee();
//Person ref point to a Student object
Person ref = studObject;
//getName() method of Student class is called
String temp = ref.getName();
System.out.println(temp);
//Person ref point to an Employee object
ref = empObject;
// getName() method of Employee class is
called
String temp = ref.getName();
System.out.println(temp);
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 17 of 25

F0031

Abstract Classes
and Methods
An abstract class is a class that cannot be
instantiated.
It is declared abstract it may or may not
include abstract methods.
Example:
public abstract class Animal {
private String nameOfAnimal;
public abstract void speak();
public String getAnimalName() {
return nameOfAnimal;
}
public void setAnimalName(String name)
{
nameOfAnimal = name;
}
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 18 of 25

17 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
18 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Abstract Classes
and Methods
An abstract method is a method that is
declared without an implementation (no curly
braces and no method statements).
To create an abstract method, write the
method declaration without the body and use
the abstract keyword.
Example:
public abstract void someMethod();

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 19 of 25

F0031

Abstract Classes
and Methods
Coding Guideline:

Use abstract classes to define broad types of


behaviors at the top of an object-oriented
programming class hierarchy, and use its
subclasses to provide implementation details of
the abstract class.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 20 of 25

19 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
20 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Interfaces
collection of constants and method
declarations

method declarations do not include an


implementation (there is no method body)

reference type, similar to a class, except that


all of its methods (if any) are implicitly
public and abstract, and all of its data (if
any) is implicitly public, static, and
final
NOTE: Interfaces demonstrate polymorphism
as well, because program may call an interface
method, and the proper version of that method
will be executed depending on the type of
object passed to the interface method call.

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 21 of 25

F0031

Interfaces
Reasons for using interfaces:

To have unrelated classes implement similar


methods

To reveal an objects programming interface


without revealing its class

To model multiple inheritance which allows a


class to have more than one superclass

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 22 of 25

21 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
22 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Interfaces
To create an interface, we write:
public interface interfaceName {
//constant definitions
//method declarations (without
implementations)
}
Example:
public interface Worker {
public void work();
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 23 of 25

F0031

Interfaces
To use an interface, you include the keyword
implements and the interface name in the
class header.
The syntax for implementing interfaces is:
public class ClassName implements
interfaceName {
//code here
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 24 of 25

23 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
24 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

F0031

Interfaces
Example:
public class Cat implements Predator {
public boolean chasePrey(Prey p) {
//code to chase prey p
}
public void eatPrey (Prey p) {
//code to eat prey p
}
}

Inheritance, Polymorphism, and Interfaces

* Property of STI
Page 25 of 25

25 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________