Beruflich Dokumente
Kultur Dokumente
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) .
* Property of STI
Page 1 of 25
F0031
Inheritance
Sets of phrases to describe the inheritance
relationship
Parent
Child
Superclass
Subclass
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
* Property of STI
Page 3 of 25
F0031
Inheritance
Benefits of Inheritance in OOP: Reusability
* Property of STI
Page 4 of 25
3 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
4 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
* Property of STI
Page 5 of 25
F0031
}
class Student extends Person
{
public Student() {
System.out.println(Student
Information);
}
* Property of STI
Page 6 of 25
5 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
6 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
* Property of STI
Page 7 of 25
F0031
Example 1:
class Student extends Person
{
public Student() {
super(SomeName,
SomeAddress);
System.out.println(Student
Information);
}
* Property of STI
Page 8 of 25
7 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
8 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
* Property of STI
Page 9 of 25
F0031
Example 2:
class Student extends Person
{
public Student() {
super.name = somename;
super.address = someaddress;
}
* 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.
* 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);
}
}
* Property of STI
Page 12 of 25
11 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
12 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
Method Overriding
Example 2:
public class Person
{
* Property of STI
Page 13 of 25
F0031
Method Overriding
Rules apply to overridden methods:
* Property of STI
Page 14 of 25
13 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
14 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
}
Example:
public final class Person {
* Property of STI
Page 15 of 25
F0031
}
Example:
public final String getName() {
return name;
}
* 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);
}
* 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;
}
}
* 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();
* Property of STI
Page 19 of 25
F0031
Abstract Classes
and Methods
Coding Guideline:
* Property of STI
Page 20 of 25
19 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
20 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0031
Interfaces
collection of constants and method
declarations
* Property of STI
Page 21 of 25
F0031
Interfaces
Reasons for using 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();
}
* 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
}
* 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
}
}
* Property of STI
Page 25 of 25
25 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________