Home > Java > javaTutorial > body text

Java Basics - Inheritance

巴扎黑
Release: 2017-06-26 11:29:16
Original
1154 people have browsed it

Before learning Java inheritance, we would like to recall several key points that need special attention to Java object-oriented.

Object-oriented simplifies complex things. It makes code more reusable and safer through encapsulation. Usually we have to learn to think in an object-oriented way and describe things or behaviors.

If you want to use OOP, you must be aware of its three main characteristics:

1. The behavior of the object: what operations you can apply to the object, or what methods you can apply to the object .

2. The object's state (state): How the object will respond when those operations are applied.

3. Object identification (identity): How to identify different objects with the same behavior and status.

All object instances of the same class have family similarities because they support the same behavior. The behavior of an object is defined by callable methods. Additionally, each object maintains information describing its current characteristics. The state of an object may change over time, but the change will not be spontaneous. Changes to the object must be achieved by calling methods.

1. Relationship between classes

* Dependency ("uses-a")

* Aggregation ("has-a")

* Inheritance ("is-a")

Here I will give a small chestnut from our daily shopping to briefly explain the differences between these three relationships. We placed an order on a certain treasure, and some nouns will appear in the order processing system:

For example: Item, Order, Shipping address, Payment ( payment), account (Account), etc. They are set into different classes by programmers.

Dependency is like a method of one class manipulating an object of another class. When you place an order, the Order class may use the Account class because the Order object needs to access the credit information of the Account object. (You can think of big data or bank credit lending)

Aggregation, aggregation means that objects of class A contain objects of class B. Just like an Order object must contain Item objects.

Inheritance, just like the RushOrder class is inherited from the Order class. Below we will introduce the relevant knowledge points of inheritance in detail.

2. Important explanation of inheritance

In my opinion, inheritance is a positive and optimistic attitude that dares to embrace change. specific implementation methods. It is the process of creating new classes from existing classes. From inheritance, you can see the derivation, upgrade and expansion of products.

1. Inheritance syntax

The keyword extends indicates that the new class is derived from an existing class. This existing class is called the parent class, and the derived class is called the child class.

For example:

class A extends B {
}//A类派生于B类
Copy after login

2.继承中的初始化顺序

简单来说,类的内部结构常见形态有四种:

1)属性(包括类属性和实例属性)

2)方法(包括类方法和实例方法)

3)构造器

4)初始化块(包括类的初始化块和实例的初始化块)

对于继承中的初始化顺序,可分为类的初始化和对象的初始化。

1)继承中类的初始化:(前提:父类中的属性和方法没有private修饰)

在JVM装在类的准备阶段,先为类的所有类属性和类初始化块分配内存空间。并在首次初始化阶段苏红为其进行初始化,类属性和类初始化块之间的定义时的顺序决定了其初始化的顺序。若存在父类,则首先初始化父类属性和类初始化块,一种上溯到Object类最先执行。

2)继承中对象初始化:(前提:父类中的属性和方法没有private修饰)

在new创建新对象时,首先对对象属性和初始化代码块分配内存,并执行默认的初始化。如果存在父类,则先搞好父类的。然后执行父类构造器中的初始化程序,接着才开始对子类的对象属性和初始化执行初始化。

3.继承中的隐藏

访问控制符的带来的访问权限,在上一篇的博客中我已经用表格的形式展示了。这里不再赘述。

当子类继承父类,子类可以继承父类中具有访问控制权限的属性和方法(不要被private修饰就可以了),对于用了private修饰的的父类所有的属性和方法,子类就继承不来了。

当子类需要改变继承过来的方法时,就需要用到重写方法了。一旦重写后,父类的方法对子类来说就表现为隐藏。你再想调用,就将调用重写后的子类当中的方法了。

当你重写了,还想调用父类,可以用两种方法:

1)将子类对象类型强制转换为父类类型,进行强调;

2)通过super.调用,注意它一定要被写到第一行。格式:super.method()

4.注意事项总结:

1)在子类构建对象时,先构建父类,再构建子类。

2)在方法调用的时候,永远永远永远是先在子类中找,子类中如果没有,再去父类中找。

代码示例:

1 public class JiC {2     public JiC() {3         System.out.println("我是父类");4     }5 6     public void method() {7         System.out.println("我是父类中的一个方法");8     }9 }
Copy after login
 1 class B extends JiC { 2     public B() { 3         System.out.println("我是子类"); 4     } 5  6     public void method() { 7         System.out.println("我是子类中的一个方法"); 8     } 9 }10 11 public class JiC2 {12     public static void main(String[] aegs) {13         System.out.println("上面示例的打印结果是:");14         B b = new B();15         b.method();16     }17 }
Copy after login

The above is the detailed content of Java Basics - Inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template