3. The basic data package type

A succession

1. What is inherited?

  • To improve the code reusability when inheritance
  • The method of call purpose codes to reuse the need to use the same process to the function module package.

2. How inheritance?

Use the keyword extends achieve, class B extends A, class B inherits from class A. If a class does not inherit other classes displayed, the default class inherits from the Object class. In java only direct and indirect parent parent relationship, not brothers. Java's single inheritance is inherited, we can achieve more than realized .

3. subclass inherits the parent class of those?

  • Modified private, are not inherited, because private modifications represent only visible in this class, subclass is not visible. The modified public can be protected and inherited by subclasses, by default modifier modified only inherited in the same package.
  • You can not inherit the parent class constructor
  • Modified static can not inherit
    • Subclass inheritance is not static variables and methods of the parent class. Because this is the class itself. But subclass is accessible.
    • Subclasses of the parent class and method of the same name and static variables are independent, there is no any relationship rewritten.

Second, polymorphism

1. polymorphic premise

  • A reference point to the parent class subclass object
  • The inheritance
  • Method overrides

2. The process of rewriting and method overloading

2.1 method overrides

Subclasses override inherited from the parent class's method to comply with two small two same principle.

  • The same method name
  • The same parameter list
  • Method returns a value the same or less
  • Method throws an exception or less the same type
  • Or more modifiers in the same manner

2.2 Method overloading

The same class, but different from the same list of parameters is the method of method overloading method name two.

3, polymorphic type

Runtime polymorphism: method overrides

Compile-time polymorphism: method overloading

4, this polymorphism and the super

this: an object representing the current date

super: on behalf of the parent class

Guess you like

Origin www.cnblogs.com/dearcabbage/p/11227597.html