Java object-oriented-inheritance

Source: Internet
Author: User

Overview:

1. Multiple classes of the same attributes and functions are extracted into another class, allowing classes with unique functionality to inherit this class. 2. Class and class inheritance can be realized by extends keyword

Format:

Class subclass name extends parent class name {}

Characteristics:

1, a class can have only one parent class, cannot have more than one parent Class 2, Java supports multilayer inheritance

 PackageCn.czbk.no5; Public classTestextends { Public Static voidMain (string[] args) {c C=NewC (); C.ashow (); C.bshow (); A A=NewA (); b b=NewB (); A.apshow (); System.out.println (B.A);}}classa{Private intA; Public voidashow () {System.out.println ("A class");}Private voidaprshow () {System.out.println ("A private show cannot be accessed by outsiders");}protected voidapshow () {System.out.println ("A protected Show");}}classBextendsa{protectedString a= "protected string"; Public voidbshow () {System.out.println ("B class");}}classCextendsb{}

Considerations for Inheritance

1. Subclasses can only inherit all non-private members of the parent class (member methods and member variables)

2. Subclasses cannot inherit the construction method of the parent class

3. Do not inherit for part of the function

 PackageCn.czbk.no5; Public classTESTEXTENDS1 { Public Static voidMain (string[] args) {cc CC=NewCC ();}}classaa{ PublicAA (String a) {System.out.println (a);}}classBbextendsaa{ PublicBB () {Super("abc");//to inherit the AA class if a AA has its own definition of the construction method to show the declaration in this placeSYSTEM.OUT.PRINTLN ("BB cont");}}classCcextendsbb{ PublicCC () {//the Null parameter construction method of the parent class is called by defaultSYSTEM.OUT.PRINTLN ("CC cont");}}

Relationship of member variables in inheritance

This: a reference to the current object

1, this. Member variables

2, this. Member Method (...)

3, this (...) Call yourself to construct a method

Super: Reference to the parent class object of the current object

1, super. Member variables

2. Super. Member method (...)

3. Super (...) Calling the parent class's constructor method

The relationship of inheriting the construction method of the Neutron parent class

1. Subclass constructs default access to the parent class's NULL parameter constructs, does not write anything, the subclass constructs the default first sentence is super ();

2. If the parent class does not have an empty parameter construct, the subclass can display the call to the parent class using the super (argument list) statement to construct the

3. Subclass constructs can be passed this (..) Call other constructs to allow other constructs to access the parent class constructs.

Member method relationships in inheritance

Same name: Subclass object calls a method of a subclass.

Method overrides: A method declaration that is identical to the parent class appears in the subclass, also known as method overrides, method replication.

The difference between overrides and overloads:

Features of overloading: in the same class. Method names are the same, parameter lists are different.

The overriding feature: to have an inheritance relationship. The declaration of the method is the same in the child parent class (both the method name and the parameter list are the same). When overridden, the subclass method access permission cannot be less than the access permission of the parent class method. Static can only be overridden by static.

Java object-oriented-inheritance

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.