Three major features of Java object-oriented

Source: Internet
Author: User

First, package (encapsulation)

encapsulation, also known as information hiding, refers to the use of abstract data types to encapsulate data and data-based operations, making it an indivisible whole, the data is hidden inside the abstract data, as far as possible to hide the details of the data, only to retain some interface to make it contact with the outside world

The idea of encapsulation:

    • Privatize the properties of a class
    • Provides a common method (setter & getter) to implement the call
 PackageCom.yyx.pratice;/*** JavaBean*/ Public classPerson {PrivateString name; PrivateInteger age;  PublicPerson () {Super(); }     PublicPerson (String name, Integer age) {Super();  This. Name =name;  This. Age =Age ; }    /** Package Properties*/     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

Ii. Succession (inheritance)

inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities

Inheritance is expressed as an intersection between object classes, which enables a class of objects to inherit data members and member methods of another class of objects.

If Class B inherits from Class A, then the object belonging to B has all or part of the nature of Class A (data attributes) and functions (operations), we call the inherited class A as a base class, parent class, or superclass, whereas inheriting Class B is a derived class or subclass

Note: The inheritance of classes in Java only supports single inheritance: A class can inherit only one parent class. Conversely, a parent class can have more than one child class, and an interface can have more than one parent class, that is, an interface can be multiple-inheritance

Subclasses instantiate objects to first create objects of the parent class

 PackageCom.yyx.pratice; Public classPerson {PrivateString name; PrivateInteger age;  PublicPerson () {Super(); System.out.println ("I am the null parameter constructor of the parent class"); }     PublicPerson (String name, Integer age) {Super();  This. Name =name;  This. Age =Age ; System.out.println ("I am a constructor of the parent class"); }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }     Public voideat () {System.out.println ("Eat!"); }     Public voidWalk () {System.out.println ("Walking!"); }}
View Code
 PackageCom.yyx.pratice; Public classStudentextendsPerson {PrivateString Stuno;  PublicStudent () {Super(); }     PublicStudent (string name, Integer age, String Stuno) {Super(name, age);  This. Stuno =Stuno; }     PublicString Getstuno () {returnStuno; }     Public voidSetstuno (String stuno) { This. Stuno =Stuno; } @Override Public voideat () {Super. Eat ();//Displays the Eat () method that invokes the parent classSystem.out.println ("I rewrote the Eat () method of the parent class")); }     Public voidStudy () {SYSTEM.OUT.PRINTLN ("Subclass's Own study () method"); }}
View Code
 Package Com.yyx.pratice;  Public class Javapratice {    publicstaticvoid  main (string[] args) {        Student Student=new  student ();        Student.eat ();        Student.walk ();        Student.study ();                Student stu=new Student ("John Doe", "1232");        Stu.eat ();        Stu.walk ();        Stu.study ();    }}
View Code

Operation Result:

I am the null parameter constructor of the parent class
Eat!
I rewrote the Eat () method of the parent class
Walk!
Subclass's Own study () method
I'm the parent constructor of the class.
Eat!
I rewrote the Eat () method of the parent class
Walk!
Subclass's Own study () method

Tri-polymorphic (polymorphism)

polymorphism , which can be understood as a variety of forms of a thing's phenotype. Two forms of representation: 1) method overloading and rewriting 2) the polymorphism of the subclass object

the polymorphism of the subclass object, the reference to the parent class, points to the subclass object, the parent class reference can only invoke methods and properties that exist in the parent class, and cannot call the extended portion of the subclass, because the parent class refers to the parent class that inherits the object of the heap.

However, if you force a superclass to be converted to a subclass, you can call a method that is newly added in the subclass that is not in the superclass, and a method in the parent class can be called by a reference to the parent class only if it is defined in the parent class and is not overridden in the subclass.

For a method defined in a parent class, if the method is overridden in a subclass, then a reference to the parent class type invokes this method in the subclass, which is the dynamic connection

The precondition for the polymorphism of the subclass object: ① to have class inheritance or implementation ② to have subclasses override the parent class method

The program runs into a compilation state and a running state. For polymorphism, at compile time, "look to the left", this reference variable is understood as the type of the parent class; At run time, look to the right, focus on the entity of the real object: The object of the subclass. Then the method of execution is the subclass rewrite

 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) { person person=NewStudent (); Person.eat ();//You can only invoke the Eat () method of a child class that has overridden the parent class        if(PersoninstanceofStudent) {Student STU1=(Student) person; /** methods to call subclasses*/Stu1.study ();        Stu1.walk (); } Behavior Behavior=NewStudent (); Behavior.walk ();//You can only call the walk () method of an interface behavior that has been overridden by a subclass        if(BehaviorinstanceofStudent) {Student STU2=(Student) behavior; /** methods to call subclasses*/Stu2.study ();        Stu2.eat (); }    }}classStudentextendsPersonImplementsBehavior {@Override Public voideat () {Super. Eat (); } @Override Public voidWalk () {System.out.println ("People have to walk."); }     Public voidStudy () {SYSTEM.OUT.PRINTLN ("Students to learn"); }}classPerson { Public voideat () {System.out.println ("Everyone has to eat."); }}InterfaceBehavior {voidwalk ();}

Three major features of Java object-oriented

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.