iT邦幫忙

2022 iThome 鐵人賽

DAY 21
0
自我挑戰組

30天Java由淺入深系列 第 21

Day 21 : Inheritance ( 1 )

  • 分享至 

  • xImage
  •  

Intro

The next two days will introduce a very core concept of object-oriented programming: Inheritance

Inheritance is a method used in classes that simply means to add code while retaining the original content.

Since we mentioned “inheritance”, we can also cite the data structure that is also a very well-known concept: the binary tree (B tree).

Binary trees themselves use the concept of inheritance, with child nodes inheriting from their parent nodes, and object-oriented programming also uses a similar concept.https://ithelp.ithome.com.tw/upload/images/20221006/20151216xLUNOSM7w7.png

As we mentioned earlier, in Java, there is “unidirectional inheritance”:
a parent class can be inherited by multiple child classes; however, a child class cannot inherit 2 or more parent classes at the same time.

To sum up:

  1. The original attributes and function variables of the parent class will be inherited by a class.
  2. The child class will also add its own attribute variables, etc., which can be said to be an extension of the parent class.
  3. However, we usually explain that the child class is an extension of the parent class.

Concept

Declare Inheritance Types

The keyword extends is used to declare that one class inherits from another.

class School{
	/*...........
     description
    ...........*/
}

class Student extends School{
	/*......................
     description in School
	......................*/

	/*............
	 description
	............*/
}

The child class (Student) inherits the content of the parent class (School) and adds a new program.
-> The last instruction for class inheritance is extends + parent class name.

  • When we want to use a member of the parent class in the main program, we can directly create an object of the child class to call it.
    However, there are two situations:
  1. private variables in the parent class
  2. the parent class's constructor

These cannot be directly inherited by the child class. Private variables, for example, need to be accessed using the get and set concepts within the package.


Access Modifier Inheritance

Here we introduce the previously mentioned prefix modifier protected that can be inherited by the quilt category

class School{
	protected String name = "Chi";
    protected float warning;
	public void show_Sname(){
		System.out.println("Personal Information : " + name);
	}
}

class Student extends School{
	public void show_warning(int late){
        warning = late / 3;
		System.out.println("You hava " + warning + " warnings");
	}
}

public class Main{
	public static void main(String[] args){
	Student paper = new Student();
    int late = 21;
		paper.show_Sname();
		paper.show_warning(late);
	}
}
  • Program analysis:
  • Subclass (Student) inherits the variable functions in the parent class, and its variable modifier is protected. Its access permissions can be granted to subclasses and classes in the same package. Assuming that this is changed to the default mode, it cannot be inherited by subclasses.
  • In the main program, we only need to create an object of Student to use the properties inherited from the parent class.

This chapter focuses on what inheritance is and how to use it. The next chapter is also about inheritance, but it will introduce more advanced topics such as how private is inherited and explain different inheritance concepts using object-related keywords.


上一篇
Day 20 : Encapsulation
下一篇
Day 22 : Inheritance (2)
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言