2. Java Object Oriented (8) _ Inheritance Thought - Inheritance Relationship

2018-05-01

Work is to better enjoy life.

 

inherited ideas

 

The concept of inheritance

  Inheritance is one of the most prominent features of object orientation. Inheritance is a new class derived from an existing class. The new class can absorb some data attributes and behaviors of the existing class, and can extend new capabilities.

  Inheritance means that the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and method of the parent class.

  Inheritance is a thing in which a subclass obtains the right to use the public/protected variables and methods of the parent class by inheriting the parent class.

 

  Inherited class: super class (parent class/super class/base class/extended class)

  sub class: subclass/extended class  

 

  Parent class: used to store commonality (common features (state and behavior))

  Subclass: used to store features (its own unique features (state and behavior))

 

 

  Inheritance can solve the problem of code duplication, the real role is to express a system.

 

The inheritance relationship

 

1. What is inheritance relationship?

  Based on a parent class's definition of an object (the definition here includes fields, methods, and inner classes), a new class definition is generated. The subclass can inherit some of the original definitions of the parent class, or add the parent class. The original definition does not have, or overrides (modifies) some features in the parent class.

 

  From an object-oriented perspective, inheritance is a general-to-special relationship , which is logically an is-a relationship. That is, the subclass is an extension of the parent class, which is a special kind of parent class.

  For example, a dog is a special case of an animal, and a dog belongs to an animal. For example, the red rabbit horse is both a horse and an animal.

   -----------------------------------------------------------------------------------------------------------------------------

 

2. Class inheritance format

  In Java, the extends keyword can be used to declare that a class inherits another class. The general form is as follows:

 

   class parent class name  {
  
   }
 
     class subclass class name  extends parent class class name  {
    // Write your own unique state and behavior
     }
  
  Look at the picture:
  
  

  ---------------------------------------------------------------------------------------------------------------------------

 

3. Classification of inheritance

  In Java, the inheritance relationship between classes and classes only allows single inheritance, not multiple inheritance. Single inheritance means that a subclass can only inherit from one parent class. Multiple inheritance means that a subclass can have two or more superclasses. That is to say, class A can only have one direct parent class, and class A cannot inherit class B and class C at the same time. Interfaces support multiple inheritance.

  Multiple inheritance is allowed in Java. Multiple inheritance means that, for example, class A inherits class B, and class B inherits class C, so according to the relationship, class C is the parent class of class B, and class B is the parent class of class A.

  -------------------------------------------------------------------------------------------------------------------------------------------------------------

 

4. Object class

 

  java.lang.Object

  The java.lang package does not need to be imported explicitly when used, and is automatically imported by the compiler when compiling.

  Object类是类层次结构的根,Java中所有的类从根本上都继承自Object类,即Object类要么是一个类的直接父类,要么是一个类的间接父类

  Object类是Java中唯一没有父类的类(不能自己继承自己)

  其他所有的类,包括标准容器类,比如数组,都继承了Object类中的方法。

  -----------------------------------------------------------------------------------------------------------------

 

二、子类到底继承了父类那些成员?(根据访问修饰符来判断)  

  • 父类的 public 成员继承为子类的public 成员,就好像他们直接定义在子类中一样。
  • 父类的 protected 成员继承为子类的protected 成员。就好像他们直接定义在子类中一样。
  • 父类的 包访问成员(父类和子类在同一个包中,父类使用缺省修饰符) 继承为子类的包访问成员。就好像他们直接定义在子类中一样。
  • 父类的private成员不能被子类继承,因为private只能在本类中访问,子类不能访问。但是子类对象的确包含父类的私有成员。  
  • 父类的构造器不能被子类继承,因为构造器必须和当前类名相同。  

注意:不要背诵文字,应该写代码去实践。

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325107514&siteId=291194637