Java object-oriented programming and other inheritance details, java Object-Oriented Programming

Source: Internet
Author: User

Java object-oriented programming and other inheritance details, java Object-Oriented Programming

This article describes the inheritance of Java object-oriented programming. We will share this with you for your reference. The details are as follows:

Inheritance:Special classes have all attributes and behaviors of general classes.

Inheritance benefits:

1. Improved code reusability

2. There is a relationship between the class and the class. With this relationship, there is a polymorphism. Inheritance is the relationship between classes and classes.

Note:

1. java only supports single inheritance and does not support multi-inheritance. Multi-inheritance poses a security risk: when multiple parent Classes define the same function but have different functions, the subclass does not know which function to run.

2. When a subclass inherits the parent class, it inherits all the methods and attributes of the parent class and can be directly used.

3. java supports multi-level inheritance, that is, sun-child-parent relationships.

Syntax:

[Class modifier] class subclass name extends parent class name {statement ;}

For example:

Class Pserson {int age; String name; public void speak () {System. out. println ("Hello World! ") ;}}// Inherits the Person class and all methods and attributes of the parent class. class Student extends Pserson {public void study () {System. out. println (" Good Study! ") ;}}// Inherits the Person class and all methods and attributes of the parent class. class Worker extends Pserson {public void work () {System. out. println (" Good work! ");}}

How to use functions in an inheritance system (refer to the api documentation ):

View the features of the parent class and create sub-class objects.

These three scenarios are often encountered during the inheritance process:

1) variable with the same name

1. If the subclass contains a non-private member variable of the same name, the subclass accesses the variable of this class, use this; the subclass accesses the variable of the same name in the parent class, and use super.
2. this indicates the reference of this class object.
3. super indicates the reference of the parent class Object (the usage is the same as this)

2) functions with the same name

1. If the subclass has the same function as the parent class (the function name and parameter are the same), when the subclass object calls this function, the content of the subclass function will be run ., The function of the parent class will be overwritten (also called rewrite ).

2. Rewrite definition: When a subclass inherits the parent class, it follows the function of the parent class and goes to the subclass. However, although the subclass has this function, the function content is inconsistent with the parent class. In this case, there is no need to define a new function, but to use the override feature to retain the Function Definition of the parent class, and rewrite the function content.

3. Considerations for rewriting (overwriting:

<1> If a subclass overwrites the parent class, you must ensure that the permission of the subclass is greater than or equal to that of the parent class. Otherwise, compilation fails. (Public> do not write rhetorical keywords> private)
<2> static data can only be overwritten.
<3> overload: only query the parameter list of functions with the same name. Rewrite: the sub-parent class methods must be the same (function name and parameter list)

Class Fu {// public void show () when the parent class is show (), it will be the same as the subclass function. The show function of the parent class will be overwritten by public void show (String name) // The show function of the parent class is different from that of the subclass (the parameter list is different), so the show function of the parent class will not be overwritten {System. out. println (name) ;}} class Zi extends Fu {public void show () {System. out. println ("zi") ;}} class Jicheng {public static void main (String [] args) {Zi z1 = new Zi (); z1.show ("nihao "); // The show function of the parent class will be called }}

3) constructor

1. during initialization of the subclass object, the constructor of the parent class will also run, because the first line of the constructor of the subclass has an implicit statement super () by default ()

2. super () accesses the constructor of the null parameter of the parent class, and the first line of all constructor In the subclass is super () by default ()

3. Why must the subclass access the parent class constructor?

<1> because the data subclass in the parent class can be directly obtained, the subclass is created to first check how the parent class initializes the data. Therefore, when the subclass is initialized, by default, the constructor of the parent class is accessed first.
<2> to access the constructor formulated by the parent class or the constructor without null parameters of the parent class, you can manually define the super statement.
<3> Of course, You can manually specify this statement in the first line of the constructor of the subclass to access the constructor of this class. However, at least one constructor In the subclass will access the constructor of the parent class.

Class Fu {String name; int age; Fu () {System. out. println ("Hello Fu");} Fu (String name) {System. out. println (name);} Fu (String name, int age) {this. name = name; this. age = age; System. out. println ("name:" + name + ", age:" + age) ;}} class Zi extends Fu {// Zi () {System. out. println ("Hello Zi");} by default, the sub-constructor Zi () {super ("zhangsan", 20) of the parent class is called first ); // manually use the super statement to specify the constructor of the parent class to obtain the non-private information of the parent class. System. out. println (name + ":" + age) ;}} class Test {public static void main (String [] args) {Zi z1 = new Zi ();}}

Example of constructor exception:

Write Program Results

Class Super {int I = 0; public Super (String s) {I = 1 ;}} class Demo extends Super {public Demo (String s) {I = 2 ;} public static void main (String [] args) {Demo d = new Demo ("yes"); System. out. println (d. i) ;}// compilation failed because the constructor with null parameters is missing in the parent class. // Or the subclass should use the super statement to specify the constructor in the parent class to be called.

Example of rewriting and overloading:

class Demo{   int show(int a,int b){return 0;}}

The following functions can exist in the subclass of the Demo.

A. public int show (int a, int B) {return 0 ;}// Yes, overwrite.
B. private int show (int a, int B) {return 0 ;}// No. The permission is insufficient.
C. private int show (int a, long B) {return 0 ;}// yes, and the parent class is not a function. Not overwritten, which is equivalent to heavy load.
D. public short show (int a, int B) {return 0;} // No, because the function cannot appear in the same class as the given function or in the Child parent class.
E. static int show (int a, int B) {return 0 ;}// No, static only covers static. <Br> therefore, subclasses can be rewritten and reloaded.

For more java-related content, refer to this topic: introduction to Java Object-Oriented Programming and advanced tutorials, Java data structure and algorithm tutorials, Java DOM node skills summaries, Java files and directories, and java cache operation tips

I hope this article will help you with java programming.

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.