The java object-oriented inheritance.

1: inheritance definition:

  Java object-oriented inheritance is one of the most significant features. Inheritance is derived from an existing class in a new class, a new class can absorb data attributes and behavior of an existing class, and can extend new capabilities.

2: Keyword: extends: Inheritance

3: Format form: public class A extends B {} A: Representative subclass B: Representative parent 

4: public subclasses inherit the parent class, protected members.

5: java Inheritance is single inheritance, with a single root, transitive. So a class only one parent.

6: Method override: (subclass overrides methods of the parent class)

  Its rules:

  1: subclass return type, method name, and parameter list must be identical to the parent class;

  2: access modifier must be greater than the range of the parent class modifier;

  3: subclasses override exceptions can not throw more than than the parent class.

With the difference of method overloading

 Rule: two with the same name and the method described above, a list of different parameters in a class.

7: super Keywords: parent superclass

8: final keyword

  1: final category: No subclass, can not be inherited

  2: final Method: can not be rewritten

  3: final constants: constant values ​​can not be modified

9: Inheritance advantages:

  1: code reusability

  2: parent class attributes and methods may be used for the subclass

  3: Designing applications easier

  4: Subclasses inherit attributes and methods of the parent class

Code:

public class Fu {
    int a=1;
public void show(){ System.out.println("父类"); } }
public class Zi the extends Fu { int A = 2 ; public void Geta () { int = A. 3 ; // System.out.println (A); // . 3 // System.out.println (this.a); / / 2 // Representative parent class object, use the subclass System.out.println ( super.a); // . 1 } public void Show2 () {System.out.println ( "subclasses" );}}
public class demo01 {
 // inherited neutrons when the parent class of the same name of the variable; if their useful for their subclasses, with no parent public static void main (String [] args) {zi and Zi = new new Zi (); // System.out.println (zi.a); zi.geta (); zi.show (); zi.show2 ();}}

The output is:

Guess you like

Origin www.cnblogs.com/-lwl/p/10964180.html