Java Learning Notes (ix)--Inheritance

Source: Internet
Author: User

I. Inheritance 1, Concept:

Inheritance is a relationship between classes and is a relationship of "is a"

Ps:java is a single inheritance

2. Advantages:

(1) Subclasses directly have all the properties and methods of the parent class (except Privata)

(2) Implementing code Reuse

3. Grammar rules:

4, a demo

(1) Animal type

 Package com.cnblogs;  Public class Animal {    publicint  : Age    ;  Public String name;      Public void Eat ()    {        System.out.println ("eat");}    }

(2) Dog type

 Package com.cnblogs;  Public class extends Animal {    }

(3) Use

 Package com.cnblogs;  Public class Init {    publicstaticvoid  main (string[] args)     {        //  TODO auto-generated method stub        new  Dog ();        Dog.eat ();         = "Sdfsdf";         =;    }}

Subclasses can use the parent class directly except for members other than private.

Second, the method of rewriting 1, definition:

If the child class is not satisfied with the method of the parent class, you can override the parent class's method, which takes precedence over the subclass's method when invoked.

2. Grammar rules:

(1) Return value type (2) method name (3) parameter type and number

Is the same as the method inherited by the parent class, which is the override of the method

Iii. sequence of inheritance initialization

1. First initialize the parent class object and then initialize the subclass object

2. Initialize the properties in the object before performing the initialization in the constructor method

Java Learning Notes (ix)--Inheritance

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.