Java Object-oriented inheritance

Source: Internet
Author: User

Inheritance in Java:

First, we want to understand when to use inheritance: inherited subclasses and parent classes are is-a relationships, for example: Apple is a fruit. Use fruit as the parent class, and Apple as a subclass of fruit.

Inherited syntax:

< access modifier > Subclass class Name extends parent class name {    subclass attribute;   subclass method;};

When we use inheritance, the parent class can inherit from the quilt class in addition to the constructor method.

use of Super :

Calls the constructor methods, properties, and methods of the parent class; The format of the invocation constructor is: Super (); The format of the invocation properties and methods is: Super. Properties/Methods;

When the constructor method is called,super () is placed in the constructor of the subclass and placed in the first row , and the properties and methods of the parent class are not restricted, but are placed in the properties and methods of the subclass.

override of Method :

overriding meaning: The subclass provides a method in which the name is exactly the same as the method name of the parent class, but the implementation is different, and the method in place of the parent class is executed when called to the method.

Method overrides the 3 criteria:

(1) The rewriting of the method occurs in the inheritance relationship;

(2) The method name of the subclass is the same as that of the parent class;

(3) The subclass method must have access rights greater than or equal to the access rights of the parent class method.

Final class:

If a class is defined as a final class, then this class cannot be inherited.

Grammar:

Final  class name {};

Abstract class:

Abstract classes are classes that are decorated with the abstract keyword.

Grammar:

Abstract  class name {};

Abstract class to pay attention to three points:
(1) Abstract classes cannot be instantiated;

(2) Abstract classes can have abstract methods and common methods;

(3) An abstract class must be inherited to be used, and subclasses must implement an abstract method in an abstract class, unless the subclass is also an abstract class.

Access modifiers in Java:

Access adornment into class subclasses different subclasses of different packages non-subclasses

Public y-y y y

Protected y y y N

Default y y n n

private n n n N

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.