Java4android (inheritance)

Source: Internet
Author: User

What is inheritance?

In object orientation, inheritance is a class that obtains the member variables and member methods of another class.

Java only supports single inheritance and does not allow many inheritance

 

Inheritance is used to reduce repeated code and facilitate modification.

Example:

Parent Person3

Class Person3 {String name; int age; void eat () {System. out. print ("dinner");} void introduce () {System. out. print ("My name is" + name + ", my age is" + age );}}

Subclass Student

Class Student extends Person3 {// extends inheritance; the extended Student subclass obtains the member functions and member variables in the parent class of Person3. // The subclass can be added with its own unique member variables and member functions int grade; void study () {System. out. print ("Learning ");}}

Main Function

Class TextStudent {public static void main (String args []) {Student student = new Student (); student. name = "Li Hua"; student. age = 19; student. eat (); student. introduce (); student. study ();}}

Output result:
Dinner

My name is Li Hua, and my age is 19

Learning

 

 

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.