Java Object-oriented programming (i)

Source: Internet
Author: User

Because of the common principles of Java and C + + object-oriented programming, so this time to summarize the relevant points separately, this article mainly summarizes the Java object-oriented programming.

The three main features of object-oriented programming are inheritance (inheritance), polymorphism (polymorphism), and encapsulation (encapsulation).

I. Inheritance

[Class modifier List] class class name [extends parent class name] [implements excuse list name] {class Body}

Class Employee {public int workyear;public Employee () {workyear = 1;}} Class Teacher extends Employee {public int classhour;public Teacher () {classhour = 10;}}

    1. Each class has and has only one direct parent class, but can implement multiple interfaces
    2. The constructor method of the currently defined class must call its immediate parent class's constructor in its first sentence. The format is:
      Super (List of references);
      Assuming that the program ape does not make an explicit call, the JVM will implicitly invoke the parent constructor without any parameters, i.e.super ()
    3. A type conversion is possible between a child class and a parent class
      (1) Converting subtype data to parent type data can be directly used for implicit type conversions
      (2) Converting the parent type data to subtype data requires a forced type conversion and is guaranteed to be correct at execution time.
      Correctteacher tom = new Teacher (); Employee a = Tom; Teacher b  = (Teacher) a;//runtime Erroremployee a = new Employee (); Teacher b  = (Teacher) A;
    4. The ability to use instanceof to infer that an object is an instance object that is not a class.
      Teacher a = new Teacher (); Employee B = new Employee (); Employee C = A; System.out.println ((b instanceof Teacher)); FalseSystem.out.println ((c instanceof Employee)); TrueSystem.out.println ((c instanceof Teacher)); True
Two. Polymorphism of static polymorphism

Static polymorphism refers to the overloaded (overload) function of a method with the same name in the same class.

Dynamic polymorphism Dynamic polymorphism refers to the override (override) of a member method of a subclass for a member method that is also declared by its parent class. The "same statement" here refers to all but the method modifiers. Use dynamic polymorphism to invoke a method of a subtype through a reference to a parent type.

Class Employee {public int workyear;public Employee () {workyear = 1;}        Public Printinfo () {        System.out.println ("This employee had worked for" + Workyear + "years.");        } Class Teacher extends Employee {public int classhour;public Teacher () {classhour = ten;}        Public Printinfo () {System.out.println ("This employee had worked for" + Workyear + "years.");        System.out.println ("This teacher have worked for" + Classhour + "hours.");} public class Main {public static void main (string[] args) {Employee a = new Teacher (); A.printinfo ();}}

    1. Defining a member field in a subclass that has the same name as the parent class is not only related to polymorphism. and does not advocate
    2. The ability to invoke the masked member methods and member fields in the parent class with Superkeyword, namely:super.xxx
    3. Dynamic polymorphism is only for non-static member methods, that is, static member methods do not have dynamic polymorphism: When a static member method with a completely identical declaration is defined in the class body of the subclass and parent class. When a variable a of type parent refers to an instance object of a subclass, calling the static member method through variable a invokes the static member method defined in the parent class body.
Three. Encapsulation it is important to understand the access control patterns of class members and the corresponding scope of access, such as the following tables:
* The above image is from the 2nd edition of the Java Programming tutorial. Yong Junhai Authoring
At this point, the three-object-oriented features of Java are discussed, and the next blog will discuss such things as abstract classes, interfaces, and inner classes.

Java Object-oriented programming (i)

Related Article

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.