Object-oriented Inheritance

Source: Internet
Author: User

Object-oriented Inheritance

What is inheritance?

Inheritance means that a class gets the member variables and member methods of another class. Java only supports single inheritance. A subclass can only inherit one parent class, and many inheritance are not allowed. Inheritance is used to reduce repeated code during class definition.

 

Basic syntax features of inheritance:

Class subclass extends parent class

Class Person {// parent class String name; int age; void introduce () {System. out. println ("My name is" + name + ", my age is" + age );}}

 

Class Student extends Person {// inherit the parent class Person public static void main (String args []) {Student s1 = new Student (); s1.name = "James"; s1.age = 18; s1.introduce ();}}

  

The subclass Student inherits the parent class Person and has the member variables and member methods of the parent class. At the same time, it can define the member variables and member functions unique to the subclass.


Object-oriented three basic features encapsulation inheritance Polymorphism

Encapsulation, inheritance, and Polymorphism

It is mainly a class.

Encapsulation is the privatization of classes.

C ++
For example

Class time
{
Public:
Private:
Int hour; // This is the encapsulation, which is the private variable of the class.
};

Inheritance is to keep the attributes of the parent class and expand new things.
Class land_vehicle: public time {

In this way, the parent class time is inherited.

Polymorphism

Polymorphism is a technique that allows the Parent object to be set to be equal to one or more of its sub-objects, such as Parent: = Child;

Object-oriented Inheritance

This is not the case, bro. You need to make your own brains and respect the people who help you solve the problem.
 

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.