Pemrograman Berorientasi Objek : Inheritance, Method Overriding, Method Overloading, dan Super
Introduction
Designing a program carefully before the programmer actually write it is very important to reduce the cost.
Three key points of good design:
- Maintainability : fewer resources for maintenance and changes, when a software needs to be modified.
- Reusability (Code Reuse): software module can be used in more than one software which ensure faster development of new software.
- Robustness: the stability of software in extreme situations.
Small Example: Software for Chess
- A Simple Chess has different types of piece
- Each piece has their own position and how they move.
Improvement for the Chess: We want to add color for every piece
Problems :
- When adding new information, cost for changing the program is high.
- We need to add new attribute: color to every piece class (Knight, Bishop, Queen, Rock, King, and Pawn)
Idea for solution :
- Adding the same new information for every class should not be done by adding it on every class
- Keeps the same (general) information for each class, and only adds new information to a newly created class.
Understand the relationship between classes is the first step to solve this problem. There are two types of relationship between classes:
- Has - A : one class belongs to (is a part/member of) another class
- Example : A chess class has pawn, queen, rook, king, queen, and bishop inside it.
- Is - A : class X is a specialization of class Y (in other words: class Y is a generalization of class X)
- Example : pawn is a piece, rook is a piece, bishop is a piece, etc.
Inheritance
Inheritance: is A relationship
Inheritance is the process by which a new class—known as a derived class —is created from another class, called the base class. A derived class automatically has all the instance variables and all the methods that the base class has, and can have additional methods and/or additional instance variables.
A base class or a superclass is called a parent class. A derived class or a subclass is called a child class. If class A is a parent of a parent of ... class B, then A is called an ancestor of class B. If class A is an ancestor of class B, then B is called a descendant of class A. A subclass has the type of every one of its ancestor classes
The general advantages:
- Avoiding code duplication : The use of inheritance avoids the need to write identical or very similar copies of code twice (or even more often).
- Code reuse : Existing code can be reused. If a class similar to the one we need already exists, we can sometimes subclass the existing class and reuse some of the existing code rather than having to implement everything again.
- Easier maintenance : Maintaining the application becomes easier, because the relationship between the classes is clearly expressed. A change to a field or a method that is shared between different types of subclasses needs to be made only once.
- Extendibility : Using inheritance, it becomes much easier to extend an existing application in certain ways.
The concept of inheritance is implemented by using the keyword: extends
Syntax :
Example :
In Java, every class is a subclass of the class java.lang.Object
Inheritance Example
Result
Create Knight Class |
Create Rook Class |
Inheritance and Encapsulation
Java’s access specifier so far :
- public : can be accessed from everywhere (for now: different classes)
- private : can only be accessed within the same class
protected : can only be accessed within the same class, within it subclasses an within classes of the same package (explained later)
Inheritance and Encapsulation Example
Now Knight can access Piece’s protected instance variable :
Instance Variable
All non-private instance variables are inherited by any subclass :
Create setters and getters for some instance variables to the Piece Class, and modify Knight’s constructor.
Method Overriding
Method Overriding : the process to redefine the specific implementation of a method that belong to the superclass
Method Overriding :
- The return type of the method must not change
- However, there is an exception: if the return type is a class type (object-reference type), it can be changed to any descendant class of the returned type (only works in Java 5.0, more in details later)
- Access permission can be changed: from private in superclass to public in subclass (more permissive access is possible, but cannot be more restricted)
- Overriding is different than overloading, overriding a method gives a subclass an exactly the same name, type and number (and sequence) of parameters of a method.
Inherited method can be overloaded by changing its signature
Super
The super keyword in java is a reference variable which is used to refer immediate parent class object.
Sumber
Slide PBO : Inheritance
Komentar ini telah dihapus oleh administrator blog.
BalasHapusPosting Komentar