Java Object-oriented inheritance

Source: Internet
Author: User
Tags instance method modifier

In Java, object-oriented is undoubtedly the weight of Java, and today we look at one of the three main features of Java object-oriented inheritance.

First, let's look at what the structure of the class is.

Classes are primarily member properties, member methods, static properties, static methods, and construction methods (regardless of code block). member properties and member methods belong to the object, and static and static methods belong to the class, as shown in.

 

The structure of the class we know (for the back of the cushion), then formally entered our theme, inheritance!

1. What is inheritance?

(1) To improve the reusability of code

(2) The same function module is encapsulated in the method needs to be used when calling the method to achieve code reuse purposes

(3) by inheriting the same class in multiple classes to extract the same, into a new class, so that other classes and the current new class to create a relationship, to achieve the purpose of code reusability

2. How can I inherit?

Using the keyword extends, as shown below, b inherits a, a is the parent of B, and B is the subclass of a. When a class does not show inheritance from other classes, the object class is inherited by default, and object is all the parent class. In Java there is only the direct parent class and the indirect parent relationship, there is no sibling relationship, and the relationship between the sun.

With inheritance, you can use a method from the parent class, as shown in, the object of the subclass can call the method in the parent class directly without redefining the method. However, many times, because the requirements of the subclass and the requirements of the parent class are different, the method of the parent class needs to be rewritten. In this case, the code reuse rate is low. In addition, Java is a single inheritance, do not allow multiple inheritance, when a class has inherited the class will not inherit other classes, code extensibility is greatly reduced, and secondly, once the inheritance chain of the class is getting longer, the cost of maintenance becomes quite high, and when one of the classes is changed, many classes need to be changed. It might not be as fast as refactoring. Most of the time we are interface-oriented programming, this topic we can come back to talk about!

3, the parent class thing, subclass can inherit all?

No, of course not;

3.1 Inheritance issues for permission modifiers

The private decoration is not allowed to inherit, because the private modifier can only be seen in this class, the subclass is not visible, and the parent class is protected or public decorated, the subclass can be inherited; the subclass that is decorated by the default modifier can inherit only under the same package;

3.2 Inheritance issues for constructors

The constructor is not inherited by the quilt class, but the object of the subclass will default to the parameterless constructor of the parent class when it is initialized, when the parent class displays a parameter constructor, and no argument constructor is present. When a subclass inherits the parent class, it must display the argument constructor of the calling parent class. The invocation can be invoked using super (A, b);

Inheritance problem of 3.3 static modifier

Subclasses are methods and variables that do not inherit the static modification of the parent class, but can be called, and THIS.A () will error, and the common method inherits this.method_a () without error;

4, override of method (overwrite)

Overrides occur at the method level, and properties are not overridden, can be hidden, and hidden for static methods and static properties. In addition to the override is for instance methods, the subclass of the instance method cannot overwrite the static method of the parent class, the subclass's static method cannot hide the parent class's instance method, when the subclass needs to extend the function of the parent class, the method of the parent class needs to be overridden, then what conditions must be overridden? , as shown,

  

4.1, that is, the method name of the method to be overridden and the parameter list must be the same, you can add @override annotations above the method to be overridden to determine whether the correct rewrite;

4.2 That is, the return value type of the subclass and the exception type that is thrown must be less than or equal to the type of the parent class

 

4.3 The permission modifier for the method to be overridden must be greater than or equal to the permission of the parent method, or it will be an error.

Note: Overrides are for instance methods, hidden for static methods and properties (mainly caused by the same method name and property name in the parent class and subclass), which is constrained by the RTTI (run-time check), that is, when the runtime knows which class the method belongs to, and hidden from the constraint. That is, during compilation, you know which class the method and property properties belong to.

The following is an online question, mainly for rewriting and hiding, small partners can do look!!!

Package com.shsxt.test;


public class Test07 {
public static void Main (string[] args) {

Circle Circle=new Circle ();
System.out.println (Circle.name);
Circle.printtype ();
Circle.printname ();

Shape shape = new Circle ();
System.out.println (Shape.name);
Shape.printtype ();
Shape.printname ();
}
}

Class Shape {
Public String name = "Shape";


public void Printtype () {
System.out.println ("This is Shape");
}

public static void Printname () {
System.out.println ("shape");
}
}

Class Circle extends Shape {
Public String name = "Circle";


public void Printtype () {
System.out.println ("This is Circle");
}

public static void Printname () {
System.out.println ("Circle");
}
}

Answer:

Java Object-oriented inheritance

Related Article

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.