Inheritance of Java

Source: Internet
Author: User

How does ① inherit a class?
extends keyword, subclass in the former parent class after

modifier Subclass extends Superclass { // class definition section }

② What are the benefits of using inheritance?
Establish a connection between classes, increasing the reusability and maintainability of code

③ How do I call the properties, methods, and construction methods of the parent class through Super?

Super Represents the default reference to the immediate parent class object of the current object , super must appear in the methods and construction methods of the subclass
     Super. Property name
    super. Method Name ()
    super () construction method
    
Span style= "FONT-SIZE:16PX;" What are the characteristics of >④   abstract classes and abstract methods? What is the relationship between the two?
     Features:
    1, abstract classes, and abstract methods are all through abstract Keyword modifiers

abstract class Test { abstract void method ();
2. Abstract classes cannot be instantiated . Abstract classes can have no, one or more abstract methods, or even all methods can be abstract methods
3, the abstract method only has method declaration , there is no method to implement . Classes that have abstract methods must be declared as abstract classes. Subclasses must override all abstract methods to instantiate, otherwise the subclass is an abstract class

  
Relationships: Abstract methods must be abstract classes, abstract classes may not necessarily have abstract methods

What property does the ⑤ subclass inherit from the parent class?
1. Properties and methods that cannot inherit private adornments
2. Cannot inherit the parent class's constructor method

Note: 1. Properties and methods that can inherit public and protected adornments, regardless of whether the child class and parent class are in the same package

2. Properties and methods that can inherit the default permission modifier decorations, but subclasses and parent classes must be in the same package

Access modifiers This class Same package Sub-class Other
Private Ok
Default (Friendly) Ok Ok
Protected Ok Ok Ok
Public Ok Ok Ok Ok



What are the requirements that the ⑥ method overrides must meet?
1. The overriding method and the overridden method must havethe same method name
2. The overriding method and the overridden method must havethe same argument list
3. The return value type of the overridden method must be the same as that of the overridden methodThe return value type is the same or its child class
4. Overriding methodcannot shrinkOverridden by the method.access Rights

What can ⑦abstract keywords modify? What can't be modified?
Can be modified: Class (abstract Class), method (abstract method)
Cannot be modified: properties, Construction methods

⑧ it and which three keywords cannot decorate a method or class at the same time?
Private: Subclasses cannot inherit to private method
Final: The abstract method is to have subclasses rewrite, and the final decorated method cannot be overridden
Static:static can be accessed directly through the class name, but cannot access a method that is not implemented

What can ⑨final keywords modify? What can't be modified?
Can be modified: class (Cannot be inherited), method (cannot be overridden), property (constant)
Cannot be modified: Construction method

⑩ What are the calling rules for constructing methods under inheritance conditions?
1, if the subclass of the constructor method does not explicitly call the parent class through super-constructor method, and does not explicitly call itself through this method of other constructors, the system will default
Calling the non-parametric construction method of the parent class
2, if the constructor method of a subclass explicitly calls the parent class with a parameter construction method, the corresponding constructor method of the parent class will be executed instead of the parent class without the parameter construction method
3, if the subclass of the construction method through this explicit invocation of the other constructor method, then the corresponding construction method to apply the above two rules
4, if there is a multi-level inheritance relationship, when creating a subclass object, the above rules will be applied to a higher level of the parent class more than once, until the top-level parent class of the object class of the non-parametric construction method

Note: 1, in the construction method if there is this statement or Super statement, can only be the first statement

2. This and super statement call construction methods are not allowed in a constructor method

3. This or Super keyword is not allowed in the class method

4, in the instance method, this and super statement is not required to be the first statement, can coexist

The relevant knowledge points of the object class?
The object class is the ancestor of all Java classes. When you define a class, if you do not use the extends keyword, the class inherits directly from the object class, and object exists in the Lang packet
Java.lang.Object Common methods: Equals (Object obj), getclass (), toString (), Hashcode (), wait (), wait (long timeout), wait (long time Out,int Nanos)

Access modifiers

Inheritance of Java

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.