The understanding of Java encapsulation, inheritance and polymorphism of three characteristics _java

Source: Internet
Author: User

First of all, let's briefly define the 3 features:

Encapsulation: Hides the attributes and implementation details of an object, exposes only the interface, and controls the level of access to read and modify attributes in the program. The abstraction of the data and behavior (or function) combined to form an organic whole, that is, the data and operation of the source code for the organic combination of "class", where the data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming, and the user does not need to know the specifics of the implementation, but only to use the members of the class through the external interface, a specific access right. The basic requirement for encapsulation is to privatize all attributes, provide getter and setter methods for each property, and, if there is a constructor with a parameter, write a constructor with no parameters. The classes that have been written are often tested during development, so there are times when the ToString method is rewritten, but this is not necessary.

Inheritance: Code reuse is implemented through inheritance. All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly. The inherited class is called a subclass, and the inherited class is called the parent class. Subclasses cannot inherit member variables and methods that have access to private in the parent class. Subclasses can override methods of the parent class and name a member variable with the same name as the parent class. However, Java does not support multiple inheritance, that is, the ability of a class to derive from multiple superclass classes. Minimize inheritance relationships in development in order to reduce the coupling of programs.

Polymorphism: Polymorphism is divided into design-time polymorphism and Run-time polymorphism, for example, overloading is also called design-time polymorphism, and for overridden or inherited methods, the Java runtime system determines which method is called Run-time polymorphism based on the type of instance that invokes the method. All in all, the typical feature of object-oriented design is inheritance, encapsulation, and polymorphism, which are also key to the popularity of object-oriented objects.

Packaging

The default value for a property of a class in Java is not private, and to hide the property's method, you can add a private modifier to restrict access only within the class.

For private properties in a class, it is given a method (Getxxx,setxxx ()) to access the private property, guaranteeing the operation and security of the private property.

The encapsulation of the method, the exposed public, the hidden hidden.

Inheritance of Java

Inheritance is the abstraction of a class of things that have a common character.

Inheritance in Java uses the extends keyword, and the Java middle finger allows single inheritance, i.e. a class can have only one parent class.

The construction method cannot be inherited .

Overrides in Java methods

A method that inherits from the parent class is overwritten when there is a method in the subclass that has the same name as the one that is accessible in the parent class and returns the same parameter list.

Super () keyword

Super (), which indicates that super () can only be the first sentence in a constructed method when the constructor method of a subclass invokes the constructor method of the parent class.

Polymorphism in Java

There are two kinds of polymorphic mechanisms: compile-time polymorphism, Run-time polymorphism

1. Overloads of methods: overloading means that there are multiple methods with the same name in the same class, but these methods have different parameters. , so you can determine at compile time which method to call, which is a compile-time polymorphism.

2, method coverage: Subclasses can override the parent class's methods, so the same method will have a different representation in the parent class from the subclass. In the Java language, a reference variable for a base class can point not only to an instance object of a base class, but also to an instance object of a subclass, and the reference variable in an interface can also point to an instance object of its implementing class.

 public class A {public string show (D-obj) {return ("A and D"); 
obj) {return ("A and a");} public class B extends a{public string show (b obj) {return ("B and B");} public string Show (a obj) {return ("B and A")
;
} public class C extends b{} public class D extends b{} public class Test {public static void main (string[] args) {
A a1 = new A ();
A A2 = new B ();
b b = new B ();
c C = new C (); 
D d = new D ();
System.out.println ("1--" + a1.show (b));
System.out.println ("2--" + a1.show (c));
System.out.println ("3--" + a1.show (d));
System.out.println ("4--" + a2.show (b));
System.out.println ("5--" + a2.show (c));
System.out.println ("6--" + a2.show (d));
System.out.println ("7--" + b.show (b));
System.out.println ("8--" + b.show (c)); 
System.out.println ("9--" + b.show (d)); } 1--a and a 2--a and a 3--a and D 4--b and a 5--b and a 6--a and D 7--b and B 8--b and D 

When a superclass object references a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the method overridden by the quilt class.

Here we use an example to illustrate what this sentence means: A2.show (b);

Here A2 is a reference variable, is a type A, it refers to a B object, so according to the above sentence means that there is B to determine who to call the method, so A2.show (b) should be called Show (b obj) in B, the resulting result should be "B and B", But why does it have to be different from the results of the previous run? Here we ignore the following sentence "but the method called here must be defined in the superclass", so does show (B obj) exist in Class A? It doesn't exist! So this sentence doesn't apply here? So is this a wrong sentence? Not too! In fact, this sentence also implied that this sentence: it is still in the inheritance chain to call the priority of the method to confirm. So it finds show (a obj) in Class A and because B overrides the method, it calls the method in class B, or it calls the method in Class A.

The above is a small set to introduce the Java encapsulation, inheritance, polymorphism of the three major characteristics of understanding, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.