Object-Oriented (b) - Inheritance

Object Oriented (II)

Object-oriented - Inheritance

Relations between classes

  1. A class as a parameter to another method, called the two classes of dependencies. (Use-a)

  2. A class as a property of another class, called the relationship between these two classes of compositions. (Has-a)

  3. The method is a property of all class inherits from another class, called a parent-child relationship between two classes is (is-a)

Inherit / extends

Format: subclasses of the parent class extends (the base class, the superclass)

A. Access modifier in succession

The same package

Subclasses can private modifier, and other properties or methods in addition to the direct access to the parent class

Different packages
  1. Subclass objects can only access public modified

  2. Sub-category method can access the internal properties of the modified method protected, but can not access the default property method

Class parent can have multiple children, but the parent is not accessible child-earth elements

Subclass only one parent class, subclass can access the elements of the parent class

II. Constructor succession

  1. When a subclass is created, call the parent class's default constructor without parameters

    If the parent is not a class constructor without parameters, it will complain in the class name of the subclass

    Solution

    1. Add constructor with no arguments in parent class

    2. super / ultra: in super refer to this parent class in the first line requires a similar inheritance

      The method may further call the parent class by super.xxx ()

III. Transformation

  1. In the inheritance relationship

  2. There should be a reference to each other

  3. Supertype into subtypes, it should be included in the parent sub-types

    // parent subclass Animal Dog 
    Dog D = new new Dog (); 
    Animal A = D;
     // or more sub-references to each other between a parent, may be changed to new new = A Dog Animol (); 
    Dog C = (Dog ) a;
     // parent type into subtypes, should be included in the parent sub-type of

    In the above code, it is transmitted to a reference transmission, a, b, c at the same time point to the same region of the stack

    Animol a = new Animol();
    Dog d = (Dog)a;

    The code error! Not compile-time error, the error at runtime

Four .Object

The root class of all classes

Object Methods:

.toString()

System.out.println () call the default method

.equals()

equals method in object is to compare address is the same

If we need to override the equals method can refer to the String

.hashCoad()

Gets the hash value of the object

Abstract class

Keywords: abstract

format:

public abstract class 类名{}

Abstract class action:

  1. 2. 3. inherited polymorphism is implemented is implemented

    // A Class B instantiated abstract subclass
     // call by polymorphic method 
    A = A new new B ();

    feature:

    1. You can not create objects (instances of), only to inherit, but there Constructor

    2. abstract modification method is an abstract method to (); as the end.

      public  abstract  void method name ();
    3. The method of pumping a certain abstract class

    4. Abstract methods can not use private modification, nor can you use static, final modifications

    5. Implement abstract methods, a white triangle appears on the right.

interface

Keywords: interface

Features:

  1. All internal default methods are disclosed in the abstract method (also omitted when public abstract),

  2. Before Jdk 1.8, the interface has only abstract methods

  3. All variables are internal static constant / must have an initial value

Interface extends the interface extends to many, through "," split

Implement many class implements interfaces through "," split

Guess you like

Origin www.cnblogs.com/-Archenemy-/p/11967579.html