Java Basics-Object oriented

Source: Internet
Author: User

Hello everyone, here I come again, today we talk about the object-oriented java.

Remember to interview several companies before the intern position, most of the interviewers have asked me about object-oriented issues, do not know the future will not ask, it is estimated that it will not be ... (: 3[▓▓]

Let's do it right here! (? ? ?)?

There are four object-oriented features-encapsulation, inheritance, polymorphism, abstraction

Packaging:

Encapsulation Concept: The caller of the method is not aware of the specific business logic of the method, only the method's implementation is aware of the concrete implementation of the method.

Encapsulation Understanding: Java encapsulates related variables and methods into classes and encapsulates them through visibility.

Visibility has four keywords:

1.Private: The current class is visible

2.protected: Current class & This package is visible

3.default: (default) Current class & This package & outer bun category visible

4.private: All visible

The visibility of class is either public or the package is visible

generally, methods are public Property Privatization (private)

Inherited:

Concept of inheritance: Subclasses can inherit all properties and methods of the parent class, but for visibility reasons, subclasses can only use non-privatized properties and methods of all parent classes (except constructors).

※ Note that inheritance is one-way and cannot be inherited from each other.

Polymorphic:

Polymorphism is rewriting and overloading, the difference is overloading and parameters, overriding and object-related

Overloaded methods can occur in two classes, or they can occur in a class with consistent method names, different parameter types, regardless of the return value.

overrides must occur in two classes, the method name is the same, the parameter type is consistent, the method returns type one The return type is consistent.

1   //method Test embodies overloaded method Testtest embodies rewrite2   Public classA {3       Public voidTestinta) {4           returnA; 5      }   6 7       Public voidTestLonga) {8           return"AA"; 9      }     Ten   One       Public voidTesttest (intb) { A         return"B";  -     } -  } the   -   Public classBextendsa{ -       Public voidTesttest (intb) { -         return"BBBBBB";  +     } -}

Abstract:

Abstract concepts include interfaces and abstract classes.

 Public Interface a{}

Interface Features: cannot instantiate an object

Only constants

only abstract methods, no common methods

Must be fulfilled.

Interfaces are special abstract classes

Whisper to You (' @@facesymbol@@′): A class can implement multiple interfaces | |  Abstract classes can implement interfaces | |  Interfaces can inherit interfaces | | Cannot implement Interface)

 Public Abstract class a{}

Abstract class Features: Cannot instantiate an object

There are constructors (with member variables that need to be initialized)

can have constants & variables

can have common methods & abstract methods

Must be inherited, the implementation has to rewrite the abstract method

Advantages of abstract classes: can be used multiple times by ♂.

Abstract methods must be in abstract classes, abstract classes are not necessarily abstract methods.

All right! This is my little bit of Java object-oriented perspective now! Hope to be able to help everyone!
We'll see you next time!

Java Basics-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.