Java Object-oriented features

Source: Internet
Author: User

(1). Abstract: Take out the important things that need to be used and ignore the unimportant things.

(2). Inheritance: Subclasses inherit non-private members of the parent class, reuse has the same properties, and when the parent class method is not satisfied, overrides the feature attribute that the parent class does not have, and the method structure remains consistent with the parent class. (Can be reused and have its own implementation).

Benefits of Inheritance: reusability of code

Using inheritance produces a keyword super. When you create a subclass object, you call the constructor of the parent class before you call the constructor of the subclass. Sub-class construction method The first sentence must have super (); that is, the parent class constructor method is called, and if there are parameters in the constructor in the parent class, you must Super ( parameter 1, parameter 2,... ..).....) . Super keyword use method 2:super.move (), at this time super refers to the object of the parent class.

Q: Why use inheritance when there are more different points?

(3) Polymorphism:

Method Polymorphic (overloaded, rewritten)

Object polymorphism :

(more accommodating) upward transformation:

Meaning: There are generally two scenarios in which a parent class object is a return value type, one is a parent class object as a method parameter, and a method that is unique in the subclass and not in the parent class is lost.

Like whatStudentclass inherits from Personclass, then you can write this.
Person person = new Student ();
This is an upward transformation without casting, but may result in loss of precision.
Because this is Personobject, cannot call child classStudentin and out of the parent class Personmethods and properties that are not in the.

As for the benefits, that is, the benefits of polymorphism, there are generally two scenarios, one is the parent object as a return value type, and one is the parent class object as a method parameter, such asStudentand theTeacherare inherited from Person, two of the classes are replicated in the parent class . Workmethod, you can write this:
public void test (person person) {
Person.work ();

}
If you are passing in aStudentobject, then execute the Workmethod isStudentin the class Workmethod, if it isTeacheris thatTeacherof the Workmethod, you can write this when you are not sure which one to execute, or you will write two methods, and a method parameter isStudentOne method parameter isteacher.

(4). Encapsulation: Encapsulation is the process and data are surrounded, access to data only through the defined interface. Object-oriented computing begins with this basic concept that the real world can be portrayed as a series of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation hides the internal implementation mechanism of the class so that it can change the internal structure of the class without affecting the consumer, while protecting the data.

Java Object-oriented features

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.