Java4android (Preliminary understanding of succession)

Source: Internet
Author: User
Tags pear

What is inheritance?

In object-oriented, inheritance is a class that obtains member variables and member methods in another class.

only single inheritance is supported in Java, multiple inheritance is not allowed

Inheritance is used to reduce duplicate code and is easy to modify

Example:

Parent class Person3

class person3{    String name;     int Age ;         void eat () {        System.out.print ("eat");    }         void introduce () {        System.out.print ("My name is" +name+ ", my age is" + ");     }

Sub-class Student

class extends person3{    //extends inheritance; extended  student subclasses get member functions and member variables        in the Person3 parent class / subclasses can add their own unique member variables and member functions        int grade;         void Study () {        System.out.print ("learning");}    }

Main function

class textstudent{    publicstaticvoid  main (String args[]) {        new  Student ();        Student.name  = "Pear";         = +;                Student.eat ();        Student.introduce ();        Student.study ();    }}

Output Result:
Eat

My name is pear, my age is 19

Learn

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.