Java Inheritance Improvements

Source: Internet
Author: User

I. Java inheritance improvements

First, the disadvantages of multiple inheritance:
1. Inherit multiple parent classes with the same method name in the parent class, resulting in ambiguity
2. The method has the same name in the parent class, and the subclass is not overwritten and ambiguous


So, Java improvements, classes can only be single-inheritance, interfaces can inherit multiple
Only abstract methods and static constants in interfaces
1. Abstract methods can only be implemented in the implementation of the interface class, call the implementation of the method of the class
2. Static variables will determine the call relationship during compilation, and there will be a conflict at compile time to prompt the error
, and reference variables generally use the class name or interface name directly to avoid ambiguity.

1.1 Initialization of inheritance

The inheritance relationship is passed. For example, Class C inherits Class B, Class B inherits Class A, and Class C has variables and methods inherited from Class B, as well as variables and methods inherited from Class A, and can have its own variables and methods. Inherited variables and methods, although implicit, are still variables and methods of C.

The construction method is based on the method of the subclass after the superclass, and all the superclass is properly initialized before the subclass accesses it.

1.2super keywords
/*Super keyword 1. Define the parent class (Father) Class 2. Define son class extends  Father Class 3. Create a son class object note: 1.this and super are similar, can only be used with objects, and cannot be used in static contexts 2. The constructor of the subclass defaults to the first row calling the parent class parameterless constructor, implicit statement, super (), If the parent parameterless constructor does not exist, the compilation error 3. Call intelligence between constructors is placed on the first line and can only be called once. Super () and this () cannot have a constructor at the same time. If a developer customizes a class and does not display inheritance for classes, can the member function in that class use the Super Guanjianjian word? Answer: You can use, inherit from the object class, which is the parent class of all classes. Overloading (overload) and overriding (override) Overloads: Prerequisites: All overloaded functions must feature in the same class: function name is the same, parameter list is different, other irrelevant (access control, return value type) overrides: Precondition: Inheritance feature: function name same, parameter list same child The return value type of the class is less than or equal to the return value type of the parent class*/classsuper{ Public Static voidMain (string[] args) {Son bighead=NewSon (11);        Bighead.run (); System.out.println ("Hello world!"); }}classfather{intX=1; Father () {System.out.println ("This is the parent class parameterless construct"); } Father (intx) { This. x=x; System.out.println ("This is a parent class with parameter constructs"); }    voidspeak () {System.out.println ("I'm a father."); }}classSonextendsfather{intY=0; Son () {System.out.println ("This is a sub-class parameterless construct"); } Son (inty) { This. y=y+x; System.out.println ("This is a sub-class with a parameter constructor"); }    voidrun () {Super. speak ();//functions that access the parent classSystem.out.println ("I am Son" + "this year" +y+ "old"); }}

Java Inheritance Improvements

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.