Inheritance relationships in Java

Source: Internet
Author: User

1. Definition

Inheritance in Java is single, a subclass can have only one parent class , and the parent class of all classes in Java is java.lang.Object, except for this class, each class can have only one parent class;

and a parent class can have more than one child class, can be inherited by multiple subclasses;

  Java only supports single inheritance, which means that a class cannot inherit multiple classes.

Java only supports single inheritance (inheriting basic classes and abstract classes), but we can implement them with interfaces (multiple inheritance {implementation} interfaces), and script structures such as:

public class One extends Parent implements two,three,four{}

2. Attention

As a subclass, an instance of a has all the member variables of the parent class B, but for private member variables, subclass A does not have access rights, which guarantees the encapsulation of B.

Keyword extends (that is, inheriting classes), subclasses can inherit all the methods and properties of the parent class, but cannot use private (private) methods and properties.

3, instanceof "Judge---is a sub-category?" Yes true, otherwise false "

1 New children (); 2 New Parent (); 3 New Children (); // parent class declaration, subclass Creation 4 instanceof Parent); 5 instanceof Parent); 6 instanceof Parent);

The resulting results are:

true true true

4. Implements Realization class

Inheritance relationships in 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.