ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #17 : Java V - More About Class Inheritance

    • เนื้อหาตอนนี้เปิดให้อ่าน
    • 66
      0
      31 ธ.ค. 61

    Constructor and super()

    Subclass Constructors

              Let's learn to set the name and color values ​​in the constructor. There is one rule to follow when defining a constructor in a subclass: We must call the superclass constructor at the beginning of the constructor. The examples below show how to create an instance of a subclass using the constructor.

         

    Calling Superclass Constructors

              To call the constructor of a superclass use: super(). In the examples below, you can see that the program automatically calls the constructor of the superclass when a subclass is being created.


    Setting Value in Field

              Let's use the constructor to set a value for the field declared in a superclass. First of all, we have to define the constructor in both superclass and subclass, as shown below. We then pass an argument to super() in the constructor of the subclass, and finally call the constructor of the superclass.

         


    protected

    run Method

              We made a run method in the Bicycle and Car classes respectively in Java Dojo II. Let's implement these methods in this section. The run method is implemented differently for each class. So, we'll implement it in each subclass instead of defining it in the Vehicle class and having each subclass to inherit it.

          

    Access to Private Fields (Review)

              If we use the run method in the program as it is, we'll get an error. This is because the run method tries to access the distance field of the Vehicle class. Like what we learned in a previous section, the Car subclass can't directly access the distance field that we set as private in the Vehicle superclass.

          

    Protected

              Let's solve this issue with a different approach than using getter and setter methods. If we use protected instead of private, we can make the field accessible only from its class and subclasses (child classes) of that class. So, setting distance field to protected will enable the run method to work properly in the Car class.

         

    Summary of Access Rights

              Let's review the following three access rights:

    · Public: content is accessible from anywhere

    · Protected: content is accessible only from the class in which it's created and subclasses (child classes)

    · Private: content is only accessible from the class in which it's created



    Abstract Methods and Classes

    Method Content "To Be Decided"

              Not limited to cars and bicycles, all vehicles have the "run" feature. As a result, all classes that inherit from the Vehicle class should have the run method. However, since each vehicle runs differently, we can't decide the content of the run method in the Vehicle class and simply have other classes inherit that from the parent class.


    Abstract Methods

              There is a way to define a method with content that has not yet been decided. By writing abstract at the beginning of the method definition as shown below, we can define what is called an abstract method. We don't write any content in the abstract method.


    Merits and Points

              An abstract method will cause an error if the subclass doesn't override (overwrite) it. So, we need to have a subclass override the abstract method. If we want to make sure that a certain method added in all subclasses, it's important to define an abstract method in the subclass.


    Abstract Classes

              A class with at least one abstract method is called an abstract class. It's required to put abstract before the class name of abstract classes. The abstract class is incomplete since it has an unimplimented method (abstract method) and therefore can't generate any instances.





    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน

    นิยายที่ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×