คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #16 : Java V - Class Inheritance
Class
Inheritance
Duplicate
Code in Classes
As shown in the examples below, the Car and Bicycle
classes have duplicate code, which we should avoid in order to make it easier
to edit and revise programs. Both of these classes are "vehicles".
The duplicate part indicates properties and actions common to all vehicles in
general, rather than properties unique to cars or bicycles.
Inheriting
Class Content
Object-oriented programing enables a
class to inherit the content of another class. We can remove the duplicate code
by combining the overlapping fields and methods to create a Vehicle class. Then the Car
and Bicycle classes can inherit from
that class. Let's learn how to implement this in the following section.
Inheritance
When a class inherits the fields and
methods of another class it's called inheritance. By using original fields and methods too, we can
customize the new (child) class. A class from which content is inherited is
called a superclass,
and the child class that inherits content is called a subclass.
extends:
Method Inheritance
We use extends when defining a subclass using inheritance. A subclass is defined as follows: class SubclassName extends SuperclassName. extends means "to expand" — inheritance is used to create a subclass with expanded features based on its superclass.
Superclass
Methods
Calling
Superclass Methods
The Car class (subclass) inherits the fields and methods of the Vehicle class (superclass). Therefore, although nothing is defined in the Car class, we can call an instance method of the Vehicle class for an instance of the Car class.
Customizing
Subclasses
Subclass
Fields and Methods
We can customize a subclass by
incorporating original fields and methods that aren't in the superclass. The fuel field is a unique instance field for the Car class. Let's declare the fuel field and define its getter method in the Car class. Here, we will also define the charge method created in Dojo II.
Note
about Calling Method
An instance of a subclass can call not
only its own methods but those of its superclass as well. On the other hand, an
instance of a superclass can't call methods of a subclass. Since class
inheritance is unilateral, if we call a method of a subclass from a superclass,
we get an error.
Structure
of Calling Methods
The figure below shows how a method
gets called from an instance of a subclass. It will first look for the method
in the subclass, and if it's defined in the subclass, it'll be called from the
subclass. If it's not defined in the subclass, it'll look for the method in the
superclass.
Method
Overriding
Customizing
Methods
Let's consider a situation where we
want to customize a superclass method inside the subclass that inherited it.
For example, when calling the printData
method for an instance of the Car class, we
also want to display the amount of fuel, as shown in the example below.
Overriding
We can override the content of a
method inherited from a superclass by defining another method of the same name
in the subclass. This is called overriding.
If we define the printData method in the Car class (subclass), it overrides the printData inherited from the Vehicle class.
Structure
of Overriding
Although the superclass and the
subclass have methods with the same name, the program first looks at the
subclass, and as a result, the content of the method is overwritten inside the
subclass. Here, the program calls the printData
method defined in the Car class.
Access
to Private Field
Fields other than fuel such as name, color, and distance are declared as private fields in the Vehicle class, and external classes like Car can't directly access them (= encapsulation). When you want to allow external access to private fields, use a getter method.
super
Duplicate
Code in Methods
The printData
method we overrode in the previous section contains duplicate code. Except for
displaying the fuel amount, the code in the printData
method in the Car class is the same
as in the Vehicle class. Let's reduce
redundancy by removing the duplicate code.
Calling
Superclass Methods
We can call an instance method of a
superclass from an instance of a subclass as follows: super.methodName().
By calling the printData method of the Vehicle class from the Car
class, we can remove all the code except for the part that displays the amount
of fuel.
ความคิดเห็น