Inheritance in Java

Source: Internet
Author: User

1. What is inheritance

Creates a new class based on a class that already exists. The existing class is the parent class, the new class is the subclass, the inheritance is the subclass inherits and owns the properties and methods of the parent class, and the child class has properties and methods that the parent class does not have.

A parent class, also known as a base class, a superclass (superclass), or a subclass, also known as a derived class.

2. Features of "Inheritance" in Java

A class in Java can inherit only one parent class. Unlike languages such as C + +, you can inherit multiple classes. This is one of the more easy-to-learn Java

Only non-private member properties and methods of the parent class can be inherited, private is unique to the parent class and cannot be inherited

3. The "inherited" format in Java.

"Inherit" using the keyword extends

class parent Class {} class extends parent Class {}

4. Example of "Inheritance":

 PackageJavastudy;classPerson { Public Static voidMain (string[] args) {PersonCeshi2 pp=NewPersonCeshi2 ();//instantiating subclasses PersonCeshi2Pp.name= "Monkey King"; Pp.age=500; Pp.height=100;    Pp.say (); }} Public classpersonceshi{//define parent class PersonceshiString name; intAge ; voidsay () {System.out.println ("Name:" +name+ "\ n Age:" +Age ); }}classPersonCeshi2extendspersonceshi{//define subclass PersonCeshi2, Inherit parent class Personceshi    intHeight//properties specific to sub-categories    voidSay () {//overrides the Say () method in the parent class in the subclass. Because there is no height property in the parent class, the Say () method overrides theSystem.out.println ("Name: +name+" \ n Age: "+age+" \ n Weight: "+height); }}

Output Result:

Name: Monkey King
Age: 500
Weight: 100

Note: Overriding the parent class in a subclass with the same name method, called the wrong "override", is also called "Overwrite".

A subclass has the same name method, the child class is called, and the child class is not, and the father is called.

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