Java object-oriented inheritance from where, why use, how to use?

Source: Internet
Author: User

first Look at a picture:

There are now two classes, student and workers, in order to improve reusability, build only one code, and let one class and another class produce a relationship, the relationship is: inheritance.

But we found that these two classes have no inheritance, but they have something in common, and we can find their shared type, → both students and workers are Person,person with name and age. That


Use keyword Inheritance (extends) in your code to have students and workers relate to person

Class person{string Name;int age;} Class Student extends person//student inherits the person student is the subclass person is the parent class (base class, Super Class) {void study () {System.out.println ("good Good Study");}} Class Worker extends Person{void work () {System.out.println ("hard work");}}

Inheritance Benefits: Improve the reusability of the code, so that classes and classes have a relationship, to another feature polymorphism provides a precondition.


when to define inheritance: you must guarantee that the class and class have an owning (is a) relationship. Apples are one of the fruits. A dog is one of the canine branches.


The embodiment of inheritance in Java: Java allows single inheritance. Do not directly support multiple inheritance, will be multi-inheritance in other ways to reflect. single inheritance: A subclass can have only one parent class.
Multiple inheritance: A subclass can have more than one parent class.





After writing this blog, how to feel it is too simple (I wrote),,,


Java object-oriented inheritance from where, why use, how to use?

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.