(2) centralized relationship in Java, is a, have a, inheritance, focus on inheritance

Source: Internet
Author: User
Tags modifier

Common class relationships in Java (Javacore is also described above)

1, is a relationship ()

2.has a overall and local relationship

3, the inheritance relationship is in the real world and the above two relations can not be described

Of course the most talked about is the inheritance relationship, which arises, the parent class and the subclass. There are some of the following stories between parent and child classes.

General considerations in parent-child classes

  subclasses display constructors that call the parent class using the Keyword super (), you must put it in the first row of the subclass constructor, or you will get an error.

The difference between super and this

Super is primarily used in subclasses, calling the constructor of the parent class super (), calling the parent class's property method super.x (), Super.fun ();

This is primarily used in this class to only want the current object.

override (override)

Subclasses rewrite the method that inherits from the parent class according to the actual need, the method is defined exactly as the method body is different. If the definition of two methods in the same class is identical, an error will be used.

Overloads and overrides are different

overloading (overload)

1, the premise: must be in the same class

2. Features:

Function name is the same, parameter list is different, unrelated to other (access control, return value type)

3. Different points: different number of parameters, different order, different types

override (override)

1. Prerequisites: Inheritance

2. Features

Function name must be the same, parameter column compatible return value

        The return value type of the subclass must be equal to or less than the return value of the parent class (this size, which is reflected in a parent-child relationship, i.e. the subclass return value should be the same as the parent class, or the subclass level of the parent class return value type)

about Abstract Classes

  classes with abstract functions (The function with the abstract modifier), the class must be an abstract class.

  Abstract classes are not necessarily abstract functions. Is it an abstract class to see if class has an abstract modifier?

Abstract classes cannot create objects using the New keyword

Interfaces in Java

The Java interface is actually a multi-inheritance

An interface can inherit an interface, but it is a highly pure abstract class.

Talk about polymorphism.

It can be said, because there is a succession of polymorphism, this can be well understood.

1. When the parent class and subclass have the same non-static member variable, the member variable of the parent class is accessed under polymorphic

2. When the parent class and child class have the same static member variable, a static member variable that is the parent class is accessed under polymorphic State

3. When the parent class and subclass have the same non-static method (that is, the method of the subclass parent), the member method of the subclass is accessed under polymorphic conditions

4, when the parent class and child class have the same static method (that is, the subclass overrides the static method of the parent class), the static method of the parent class is accessed under multiple sets.

  As long as it is related to static, it is the parent class first, non-static, then the subclass takes precedence.

Access rights in Java, declaring a word, the question of access rights, said private permissions are private, meaning that in the class can be seen, even if the subclass of the inheritance is not visible, only through the relevant Getset method.

(2) centralized relationship in Java, is a, have a, inheritance, focus chat inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.