Some Object-Oriented Knowledge in java (2) and java Object-Oriented Knowledge

Source: Internet
Author: User

Some Object-Oriented Knowledge in java (2) and java Object-Oriented Knowledge
1. What is encapsulation? Why encapsulation? How to Implement encapsulation?

The purpose of encapsulation is to improve program security. Encapsulation is to hide the attributes and methods that do not want third parties to see.

The encapsulation method is as follows: 1. Modify the visibility of attributes to restrict access.

2. Set the attribute reading method.

3. Add the read restriction on the heap attribute in the read attribute method.

Package com. chen. test; public class Test9 {private int age; // defines the age private String name; // defines the name private double price; // define the price // use the get/set Method to encapsulate the public int getAge () {return age;} // when the age is input, public void setAge (int age) {if (age> 100 | age <0) {this. age = 18;} this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public double getPrice () {return price;} public void setPrice (double price) {this. price = price ;}
}
Summary of several encapsulated keywords:

Private: can only be called by the class itself

Public: 1. It can be called by methods in the class itself. 2. You can directly call the public attributes and methods of the parent class in the subclass. 3. It can be called directly by objects.

Protected: 1. It can be called by the class's own method. 2. You can directly call the protected attribute and method of the parent class in the subclass.

Default keywords: 1. attributes and Methods Modified by the default keyword of the parent class can be directly called in the subclass. 2. It can be called directly by the object of this package.

Keyword comparison: subclass, Inside Package, out-of-package.

The original intention of designing the protected keyword is to design the attributes and methods of the parent class for the subclass to call.

The default keyword is designed to allow calls within the package, but not outside the package.

 

Ii. Inheritance

JAVA inheritance is a single-line inheritance, and multiple inheritance is not allowed;

Subclass can inherit the methods and attributes of the parent class:
1. The private attributes and methods of the parent class cannot be inherited.
2. The protected attributes and methods of the parent class can be inherited.
3. The public attributes and methods of the parent class can be inherited.
4. attributes and Methods Modified by default Keywords of the parent class. If the child class and parent class are in the same package, they can inherit

Abstract classes and abstract methods:
Class that cannot be instantiated is modified with abstract. This is an abstract class.
Abstract classes can write abstract methods or non-Abstract METHODS -------------- abstract methods cannot be defined in non-abstract classes;
Abstract class defined in the abstract class must be overwritten in the subclass ------ the subclass must override the abstract method of the parent class (unless the subclass is also an abstract class)
What is the role of an abstract method?
The subclass must override the abstract method of the parent class to realize polymorphism;
Summary of final keywords:
1. final modifier variable ----- give the variable an opportunity for initialization and cannot be modified later
Public final double dburl;

Final modifies the referenced variable ------------------- the referenced address is locked and cannot be changed;
Final Dog dog = new Dog ("Huanhuan ");
Dog = null;

2. final modifier class ----- This class cannot be inherited by the quilt class
Public final class Dog extends Pet {}

3. final modifier ------ indicates that this method cannot be overwritten by the quilt class.
Public final void swim (Pool pool ){}

Rewrite: override
The subclass has a method with the same name, same parameter, and same return value as the parent class. Therefore, this method of the subclass overwrites the Same Name method of the parent class;

What has been rewritten?
The essence of the method ------ >>> is a function pointer
The essence of rewriting is to replace the first address of the method with the same name in the parent class memory area with the first address of the method in the sub-class memory area.

Comparison between interfaces and abstract classes
1. The interface is a capability Representation
An abstract Class has attributes and behaviors.

2. constants can be defined in the interface, but no attribute exists.

3. All methods in the interface are public abstract (a common abstract method cannot define other types of methods)

4. Methods in abstract classes can be abstract or non-abstract. But the interface must be abstract;

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.