Java oop (some of their own understanding, and did not unfold very thin)

Source: Internet
Author: User

The content is a summary of their own, just according to their own understanding to write. Reference is a beginner's tutorial.
Java is an object-oriented language.
OOP is object-oriented programming.
Encapsulation: In some classes, some properties do not want to be exposed, but we would like to provide a way to access or modify it, what to do?
Encapsulation is now the solution to this problem:
If there is a student class

public class student{
constructor, you can write a default constructor that can be written without writing and not written by default in Java

private int number;

private String name;

public void Setnumber (int number) {
This.number = number;

public int GetNumber () {
return this.number;

public void SetName (String name) {
This.number = number;

public int GetName () {

At the moment, the two attributes of the student class are private (proprietary) and we cannot use them directly:
Student stu = new Student ();
Stu.number = 4; = "Test";

But the method is public, and we can write or obtain these two private properties by calling these methods:
Student stu = new Student ();
Stu.setnumber (4);
Stu.setname ("test");

This is the encapsulation of classes, what are the benefits of encapsulation?

1. Good encapsulation can reduce coupling.

2. The structure within the class can be freely modified.

3. More precise control of member variables is possible:
The member variables of a class can be read-only or write-only.
such as accessor getxxxx (); modifier setxxxx (); With these, you can control read and write permissions

4. Hide the information and implement the details:
For example, it's a way to get a birthday in a class.
Public String Getbirthday () {
Return year + "-" + month + "-" + day;

public void Setbirthday (String birthday) {
Split the string, remove the date, and assign the year, month, and day

And when this method is called externally
This.getbirthday ();
This.setbirthday ("1990-9-9");

At this point the external is not know how to store birthday this method, the user can only invoke it.

In Java, you can have a subclass go extend (Inherit) a parent class, and a child class can inherit only one parent class.
Subclasses inherit all the methods of the parent class.
In these methods, as long as the method name and its parameter type and quantity, only the code inside the method is rewritten, is called rewriting,
If the names of these methods are not changed, but the parameter types change, or if the number of arguments changes, or if both are changed, it is called overloading.
Overrides and overloads are part of polymorphism.

Of course it is possible that the parent class is a suction class, the subclass inherits the abstract class to add specific code to the abstract method in the parent class, and if not, the method in the subclass is declared as an abstract method, the subclass is declared abstract, and the default public.
The subclass inherits the implementation of the abstract method (you can also continue with the above scenario).

A tool in which different uses produce different results or behaviors.
More for interfaces?
Where overrides and overloads are an important part of polymorphism.
Overrides are inherited and occur between two classes (parent-child class)
Overloading occurs between a class.

Java oop (some of their own understanding, and did not unfold very thin)

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: 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.