Java face question one: object-oriented three major features

Source: Internet
Author: User

One, polymorphic:
1. Object-oriented four basic features: abstraction, encapsulation, inheritance, polymorphism
Abstraction, encapsulation, inheritance is the basis of polymorphism. Polymorphism is the expression of abstraction, encapsulation, and inheritance.
2. What is polymorphic
Different classes of objects react differently to the same message called polymorphic
3. The role of polymorphism
In simple terms: decoupling. The detailed point is that polymorphism is the basis of design patterns (since it is the basis, then some design patterns must be polymorphic in the following three conditions)
4. Three conditions for polymorphic presence
Have an inheritance relationship
Subclasses override the parent class method
Parent class reference to child class object
5. Note:
Because there must be a "subclass rewrite parent method" condition for polymorphic existence, the following three types of methods have no way of showing polymorphic properties (because they cannot be overridden)
static method: Static modification is a method that belongs to a class, not an instance
Final method: Because the final modified method cannot be overridden by the quilt class
Private method: The private decoration of the FA that the child class is not visible,
Protected method: The method that is protected modified can be seen by the quilt class, can also be rewritten, but cannot be referenced externally, can not be referenced, there is no polymorphism
6. Polymorphism Classification:
Compile-time polymorphism, method overloading
Run-time polymorphism, method overrides
7. Several principles for analyzing polymorphic problems
For example, there is a parent class father, there is a subclass children
(1) The upward transformation is automatic. Father f=new Children () is automatic and does not require a strong turn
(2) The downward transformation is stronger. That is, children c=new Father () is not compiled, it must be children c= (children) New Father () so that the parent knows which subclasses it is going to go to.
(3) The parent class reference points to the subclass object, the subclass overrides the method of the parent class, invokes the method of the parent class, and the actual call is that the subclass overrides the method of the parent class.
That is, Father f=new children (), f.tostring () actually calls the ToString () method in children
For example:

Parent class: Public classFather {Private Doublemoney=100;  Public DoubleGetmoney () {returnMoney ; }     Public voidSetmoney (DoubleMoney ) {         This. Money =Money ; }     Public voidUsephone () {System.out.println ("Using the old Age machine"); }}
Subclass: A method that has an inheritance relationship and overrides the parent class Userphone ()  Public class extends Father {    publicvoid  Usephone () {        System.out.println ("Inherit money:" +getmoney () + "Use smart machine");}    }
 Public class Test {    publicstaticvoid  main (string[] args) {        Father f=New  // the reference to the parent class points to the child class object.         F.usephone ();   is actually called the subclass of the method, output: "Inherit money:100.0 using the smart machine"    }}

Two, package
An object he encapsulates is his own property and method, so it does not need to rely on other objects to complete its own operation
Encapsulation is the privatization of an object's properties, as well as a way to provide access to the properties of the outside world.
1. Three major benefits of encapsulation:
Good encapsulation reduces coupling
The structure within the class can be freely modified
You can control the member variables more precisely
Hidden information, implementation and other details
2. Practical Application
(1) Benefit 1: Encapsulation does make it easy for us to modify the internal implementation of the class without modifying the customer code that used the class.

 Public classUser {PrivateLong ID; PrivateString name; Private intAge ;  PublicLong GetId () {returnID;}  Public voidSETID (Long ID) { This. ID =ID;}  PublicString GetName () {returnname;}  Public voidSetName (String name) { This. Name =name;}  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}

If we need to modify the user class, change the age to string. You have only one place to use this class OK, if you have dozens of or even hundreds of such places, you are not going to change to collapse. If we use encapsulation, we can simply change the Setage () method of the next husband class without having to make any changes.

 Public classUser {PrivateLong ID; PrivateString name; PrivateString age;//change to String type     PublicLong GetId () {returnID;}  Public voidSETID (Long ID) { This. ID =ID;}  PublicString GetName () {returnname;}  Public voidSetName (String name) { This. Name =name;}  PublicString getage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =string.valueof (age); }}

(2) Benefit 2: More precise control of member variables is possible.

 Public classUser {PrivateLong ID; PrivateString name; Private intAge ;  PublicLong GetId () {returnID;}  Public voidSETID (Long ID) { This. ID =ID;}  PublicString GetName () {returnname;}  Public voidSetName (String name) { This. Name =name;}  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {        if(age>150) {System.out.println ("Age does not meet the requirements");//Output hint Information}Else {             This. Age =Age ; }            }}
Benefit 3: For example gender we are generally in the database is 1, 0 way to store, but at the front desk we can not show 1, 0, here we just need to do some conversion in the Getter () method.
 Public classUser {PrivateLong ID; PrivateString name; PrivateInteger sex; PrivateString sexstring;  PublicLong GetId () {returnID;}  Public voidSETID (Long ID) { This. ID =ID;}  PublicString GetName () {returnname;}  Public voidSetName (String name) { This. Name =name;}  PublicString getsexstring () {if(sex==1) {setsexstring (Male); }Else if(sex==0) {setsexstring (Female); }Else{setsexstring (Unknown); }        returnsexstring; }     Public voidsetsexstring (String sexstring) { This. sexstring =sexstring; }}

Third, inherit
1. Using inheritance can be used to reuse code. You can put the common attributes of all subclasses into the parent class
Use inheritance to remember three words:
Subclasses have properties and methods that are not private to the parent class
Subclasses can have their own properties and methods, that is, subclasses can extend the parent class
Subclasses can implement methods of the parent class in their own way
2. Construction Method:
The first is to complete the initialization of the object by constructing the method, the subclass constructor method by default calls the parent class construction method, through super () way
(1). The parent class has a default constructor method, and the constructor of the subclass executes super () by default, and the constructor method of the parent class is invoked to instantiate
(2). The parent class does not have a default construction method (which is definitely handwritten by the constructor method), and the subclass constructor method is displayed using super () to call the parent class construction method
3. Disadvantages:
Parent class changes, subclasses must change
Inheritance is a strong coupling relationship

Java face question one: object-oriented three major 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.