The object-oriented features of the Java Foundation.

Source: Internet
Author: User

First you need to know what classes and objects are?

Wikipedia defines the class as: an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods), that is, the ability to provide an extensible template for creating object instances, providing data and behavior operations. In the implementation angle, it is stored in the memory of the method area in the form of bytecode, which is used to instantiate the object.

Wikipedia defines the object as: a location in memory has a value and possibly referenced by an identifier, that is, an area in memory that contains data and is referenced by an identifier. At the implementation angle, the class object entity is stored in heap memory and its address is stored in the object reference on the stack (that is, the object variable of the class, which points to the memory space where the object resides).

  What are the aspects of object-oriented features? (Refer to the complete question of Java face)

    • abstract : Abstraction is the process of constructing classes by summarizing the common features of a class of objects, including both data abstraction and behavioral abstraction . Abstractions focus only on what properties and behaviors the object has, and do not care what the details of those behaviors are.

    • Inheritance: Inheritance is the process of creating a new class from an existing class that inherits information, the class that provides the inherited information is called the parent class (superclass, base class), and the class that gets the inherited information is called a subclass (derived class).

    • Encapsulation : Encapsulation is generally considered to be the binding of data and the method of manipulating data, and access to data can only be achieved through defined interfaces. The essence of object-oriented is to portray the real world as a series of completely autonomous and closed objects. The method we write in the class is a encapsulation of the implementation details, and we write a class that encapsulates the data and data operations. It can be said that encapsulation is to hide all the hidden things, only to the outside world to provide the simplest programming interface.

    • polymorphism: polymorphism means that objects of different subtypes are allowed to respond differently to the same message. The simple thing is to invoke the same method with the same object reference but do something different . Polymorphism is divided into compile-time polymorphism and run- time polymorphism. If the method of an object is treated as a service to the outside world, then the runtime polymorphism can be interpreted as: when a system accesses the services provided by B system, B system has a variety of ways to provide services, but everything is transparent to a system (like an electric shaver is a system, its power supply system is B, b system can use battery power or AC power, and possibly even solar energy, a system only through the Class B object call power supply method, but do not know what the bottom of the power supply system to achieve what is the way to get the momentum. method Overloads (overload) implement compile-time polymorphism (also known as pre-binding), while the method override (override) implements runtime polymorphism (also known as post-bind). Runtime polymorphism is the most essence of object-oriented things, to achieve polymorphism need to do two things: 1). method overrides (subclasses inherit the parent class and override existing or abstract methods in the parent class); 2). Object Styling (referencing a subclass object with the parent type object, so that the same object calls the same method will behave differently depending on the subclass object).

  (1) overloading and rewriting


      • method Overloading is a means of allowing classes to handle different types of data in a uniform way, The function has the same name, but the number and type of parameters are different ;

      • When invoking a method, it is possible to use different parameters to determine which method is used to achieve polymorphism.

      • When overloaded, the function has the same name, with different parameters, and the return value type can be the same or different.


      • The polymorphism between the parent and child classes, redefining the functions of the parent class. If a method is defined in a subclass that has the same name and parameters as its parent class, the method is overridden or overwritten.

      • a method in the Kawai class is associated with a party in the parent class Method has the same method name, return type, and parameter table , the new method overrides the original method and can invoke the parent class method using the Super keyword;

      • The access adornment permission for a subclass function cannot be less than the parent class.

NOTE 1: Cannot return a value Type distinguishes overloaded functions .

Understanding from a package perspective: A method includes a return value type, a method name, a parameter list, and a method body. The method body is encapsulated by the method, and the method name and parameters are the interfaces provided externally by this package .

As for the return value is related to the method body, it is the result of the method body execution, and whether or not it is determined by the method body. In other words, there is a method body, there is a return value, it is not a package external interface , when we call the method is called the external interface (method name and parameter list), so the return value can not be used as a method of overloading.

There are exceptions, however, when the compiler can determine semantics based on context, such as int x=f (), or you can distinguish overloaded functions. Direct f (); is unable to distinguish between overloaded functions.

  NOTE 2: The presence of an interface (interface) in Java also embodies runtime polymorphism.

  Summary :

  Polymorphism can be explained in three ways, from interface implementations, overloads, and overrides:

      • interface and implementation of the separation, an interface can correspond to different implementations, to achieve polymorphism;

      • Overloading, the manifestation of polymorphism in a species, multi-state of many different methods with the same name and parameters, is a compile-time polymorphism.

      • Overrides, which inherit the resulting polymorphism, redefine the method of the parent class so that different subclass objects respond differently to the same message, which is run-time polymorphism.

  (2) object Styling (casting), styling conversion or type conversion

    • Upward transformation (upcasting), the Java sub-class object can be directly assigned to the parent class object, this time the parent object refers to the memory space of the subclass object. That is, the child-to-parent class, the parent class can perform different behaviors based on the memory space of the subclass object. The parent class object and the subclass object refer to the same object, but the compiler still considers the parent class object as the parent type.

Note: The parent Class object reference (reference subclass) does not have access to the newly added members (properties and methods) of the child class object;

can use Reference Variable instanceof class name to determine whether the object that the reference variable "points to" belongs to the class or subclass of the class.

    • Down Transformation (downcasting), when you assign a parent class object to a subclass object in Java, you perform a modeling transformation (that is, coercion of type conversions). That is, the parent-to-child class, the program can be compiled, but needs to be checked at run time, depending on whether the parent class refers to the memory space of the child class object, different results appear.

if The parent class variable refers to the correct subclass type (that is, the parent object references a subtype of memory space and then assigns the child class object), the assignment executes ; if the parent class variable refers to a child type that is not related , will produce C lasscastexception exception. For example: A is the parent class of B, a a=new B (); b b= (b) A; No exception is reported on execution. If a a=new a (); b b= (b) A; The exception is reported at execution time.

Add: Downward transformation strictly according to the above criteria, the array can also be cast to match the criteria.


Transformation example Description:

 1         object[] o={"QW", "we", "RT"};  // o An array of objects of type Object   getclass () returns class [ljava.lang.object; 2          object[] oo=new string[]{"QW", "we", "RT"};//oo is an array of type Object, but it references a subclass of String array   getclass () return class [ljava.lang.string; 3          object ooo= "QW";  //ooo is the Object variable   The default is String type     getclass () returns class  java.lang.string; 4  5         //string[]  s= (string[])  o;  //The runtime will report an exception:java.lang.classcastexception: [ljava.lang.object;  cannot be cast to [ljava.lang.string; 6          string[]ss= (string[])  oo; //Run normally  7          string sss= (String)   ooo; //is running normally, you can convert object to string 8  9          System.out.println (OO); 10         system.out.println (ss);11          system.out.println (OOO);12          SYSTEM.OUT.PRINTLN (SSS);13          System.out.println (Ooo.getclass ());

Output Result:

[Ljava.lang.string;@28d93b30[ljava.lang.string;@28d93b30qwqwclass java.lang.String

One more example:

 1 public class TestCasting{ 2     public static  Void main (string args[]) { 3         animal a  = new animal ("a");  4         cat c  = new cat ("C", "Catcolor");  5         dog  d = new dog ("D", "Dogcolor"); 6  7          system.out.println (a instanceof animal);     //true 8          system.out.println (C instanceof animal);     //true 9         system.out.println (d  instanceof animal);    //true10          system.out.println (A instAnceof dog);        //false11 12          a = new dog ("D2", "Dog2color");         //the parent class refers to the subclass object, and the 13         system.out.println is transformed upward (;                 // Access to 14         //system.out.println (A.folorColor);    //!error    does not have access to any properties beyond the animal itself 15          System.out.println (a instanceof animal);     //is an animal 16          system.out.println (A instanceof dog);         //is a dog, but cannot access the properties of the dog 17 18         dog  d2 =  (Dog) a;    //forced conversion 19         system.out.println (D2.folorColor); After     //a cast, you can access the properties of the dog 20 21          dog[] dogs = new dog[2];22         dogs [0] = new dog ("Dog1", "Black"); 23         dogs[1]  = new dog ("dog2", "yellow"); 24         animal[]  animals = dogs;25         dog[] dogs1 =   (dog[])  animals;26         system.out.println (dogs); 27          system.out.println (DOGS1);28      }29 }30 class animal{31     public string name;32      pUblic animal (string name) {33 =  name;34     }35 }36 class Dog extends Animal{37      public string folorcolor;38     public dog ( String name,string folorcolor) {39         super (name); 40         this.folorcolor = folorcolor;41      }42 }43 class Cat extends Animal{44      Public string eyescolor;45     public cat (String name,String  eyescolor) {46         super (name);47          this.eyescolor = eyescolor;48     }49  }

Output Result:


  Summary :

      • To move up, the child class object is assigned to the parent class object, the parent object points to the subclass object memory space, can access the properties and methods defined in the parent class, and cannot invoke the newly added properties and methods of the subclass;

      • A downward transition that assigns the parent class object to the subclass object, at which time an exception occurs depending on whether the space referenced by the parent class object is absolutely running;

      • Arrays are also subject to the above rules.

The object-oriented features of the Java Foundation.

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.