Java Inheritance Overview and the benefits of Java inheritance cases and inheritance

Source: Internet
Author: User

Java Inheritance Overview

1. When the same properties and behaviors exist in multiple classes, the content is extracted into a single class, so that multiple classes do not need to define those same properties and behaviors, as long as they inherit that class.

2. Inheritance of classes and classes can be achieved through the extends keyword in java.

Example: Class subclass name extends parent class name {}

3. This class alone is called a parent class, a base class, or a superclass, and these classes can be called subclasses or derived classes.

4. With inheritance, when we define a class, we can define our own new members on the basis of a class that already exists.

-----------------------------------------------------------------------------

First I'll write two code:
//Define Student class
class Student {
String name;
int age;

Public Student () {}

//getxxx ()/setxxx ()

public void Eat () {
System.out.println ("eat"); }
}

//define teacher Class
classes Teacher {
String name;
int age;

Public Teacher () {}

//getxxx ()/setxxx ()

public void Eat () {
System.out.println ("eat"); }
}

Let's look at the above two codes:
The Name,age member variables are found, as well as GetXXX ()/setxxx (), and eat () are the same.
If we continue to define classes later, for example: Worker class, military class. Do they also have these things?
So, every time we define such a class, we have to redefine the repetition.
No trouble? Sure, trouble. So, we have to consider improvements?
So how to improve it?
I think: can I define these same content in a separate class.
Then, let these multiple classes and this independent class produce a relationship, with this relationship,
These classes can have the functionality of this standalone class.
To achieve this effect, Java provides a technique: inheritance.

Father:
4 sons

What does inheritance say? What does the inherited format look like?
Class Fu {}

Class Zi extends Fu {

}

We'll go back and change our code:
Class Person {
String name;
int age;

Public person () {}

GetXxx ()/setxxx ()

public void Eat () {
System.out.println ("eat");
}
}

Class Student extends Person {
Public Student () {}
}

Class Teacher extends Person {
Public Teacher () {}
}

-----------------------------------------------------------------------------

Benefits of Java Inheritance cases and inheritance

1 /*2 Inheritance Overview:3 The same content in multiple classes is extracted to define a class. 4         5 How do you implement inheritance? 6 Java provides the keyword: extends7         8 format:9 Class subclass name extends parent class name {}Ten          One Benefits: A A: Improves the reusability of the code.  - B: Improves the maintainability of the code.  - C: Having a relationship between classes and classes is a precondition for polymorphism.  the      - The relationship between classes and classes is, in fact, a disadvantage of inheritance: - the coupling of the class is enhanced.  -          + Principles of development: Low coupling, high cohesion.  - coupling: The relationship between a class and a class.  + cohesion: The ability to do something yourself.  A */ at  - //before using inheritance - /* - class Student { - Public void Eat () { - System.out.println ("eat"); in     } -      to Public void Sleep () { + System.out.println ("sleeping"); -     } the } *  $ class Teacher {Panax Notoginseng Public void Eat () { - System.out.println ("eat"); the     } +      A Public void Sleep () { the System.out.println ("sleeping"); +     } - } $ */ $  - //after using inheritance - classPerson { the      Public voideat () { -System.out.println ("Eat");Wuyi     } the      -      Public voidsleep () { WuSystem.out.println ("Sleeping"); -     } About } $  - classStudentextendsPerson {} -  - classTeacherextendsPerson {} A  + classExtendsdemo { the      Public Static voidMain (string[] args) { -Student s =NewStudent (); $ s.eat (); the s.sleep (); theSystem.out.println ("-------------"); the          theTeacher T =NewTeacher (); - t.eat (); in t.sleep (); the     } the}

Java Inheritance Overview and the benefits of Java inheritance cases and 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.