In Java (constructor, method overload, final modifier usage, and inheritance and abstraction)

Source: Internet
Author: User

Construction Method:

The constructor method has the same name as the class name, there is no return type, and the argument list (type, number) is different

Method overloading: Both member methods and construction methods can be overloaded

The method name is the same but the argument list (type, number) is different, which becomes the overloaded method.

Inheritance: direct support for single inheritance, a class can have only one parent class

Inheritance to be decorated with the extends keyword

public class Dog extends pet{

Method body

}

  subclass calls the member variables and methods common to the parent class using the keyword supe R   like super. Method Name Super (member variable, member variable ....) )

Method overrides:

Requirements for method rewriting:

1. The overriding method and the overridden method name must be the same

2. The override method and the parameter list of the overridden method must be the same

3. The override method must be the same as the return value type of the overridden method

4. The override method cannot be less than the access permission of the overridden method

Subclasses that inherit the parent class do not satisfy the requirement, overriding the method with the same name as the parent class in the child Class (overwrite)

The difference between overloading and overriding:

Overloading: To the same method in the same class, requires the same method name, the number of lists is different, and the return value and modifier Independent

Override: Subclass and Parent class with the same name method, method name, parameter list and return value type, access modifier cannot be greater than parent class

Enforcing method overrides: Abstract classes and abstract methods:

Abstract class with keyword abstract keyword decoration

Classes that are decorated with an abstract cannot be instantiated, and the parent class method, which is decorated with the abstract, must be overridden by the subclass

Final modifier:

The final decorated class cannot be inherited

The final modified method cannot be overridden

Final modified variables (including members and parts) become constants and can only be assigned once

Deep understanding of Final click

In Java (constructor, method overload, final modifier usage, and inheritance and abstraction)

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.