Java Object-Oriented foundation

Source: Internet
Author: User
Tags volatile multiple inheritance in c

Java object-oriented four features

1. Abstraction:

Abstraction--is to ignore aspects of a topic that are not relevant to the current goal in order to pay more attention to the aspects related to the current goal. (That is, the real world of a certain kind of things, extracted from the program code, the abstract is generally called a class or interface.) Abstraction does not intend to understand all of the problems, but only to select part of them, temporarily without some detail. Abstract includes two aspects, one is data abstraction, and the other is process abstraction.

Data abstraction--the attribute of a class of things in the present world, in the form of code, is the property of the object. For example, to create a bird class, birds have the following properties: A pair of wings, two feet, feathers and so on. The abstract class is either a bird's property, or a member variable.

Process Abstraction--a series of behaviors that represent things in the real world in the form of code, that is, the behavioral characteristics of objects. For example, birds can fly, will call and so on. Abstract classes are generally the methods of birds.

2. Inheritance:

Inheritance is a hierarchical model of a junction class and allows and encourages the reuse of classes, which provides a way to articulate commonalities. A new class of objects can be derived from existing classes, a process known as class inheritance. The new class inherits the attributes of the original class, which is called the derived class (subclass) of the original class, and the original class is called the base class of the new class (The parent Class). Derived classes can inherit methods and instance variables from their base classes, and classes can modify or add new methods to make them more suitable for special needs. So it can be said that inheritance is to reuse the parent class code while preparing for polymorphism.

3. Package:

Encapsulation is the process and data is surrounded, access to data only through the defined interface. Object-oriented computing begins with this basic concept that the real world can be portrayed as a series of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation hides the internal implementation mechanism of the class so that it can change the internal structure of the class without affecting the consumer, while protecting the data.

4. Polymorphism:

Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism consists of parameterized polymorphism and inclusion polymorphism. Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name. In general, the method overrides, overloads, and dynamic links form polymorphism. One of the conceptual reasons for introducing polymorphism in Java is to compensate for the insufficiency of the single inheritance of classes. (to circumvent the complex inheritance problems caused by multiple inheritance in C + +, Java uses single inheritance.) )

Dynamic linking-for a method defined in a parent class, if the method is overridden in a subclass, a reference to the parent class type invokes this method in the subclass, which is the dynamic link.

Note: Inheritance and overloading: the first is the relationship between the subclass and the parent class, and the second is the problem of overloading the method.

Subclass objects can be used directly as parent objects, but not in the opposite way. For example, the person is the parent class, the student is the human child class, therefore the student object must have the human object the attribute, but the human object may not have the student object characteristic. So the student object can be used as a person object, but the human object cannot be used as a student object. Note that when a target class object is used as a parent class object, the subclass object loses all of its subclass attributes, preserving only properties and methods with the same name as the parent class (the same name method is not just the same as the function name, and the parameter type is the same, otherwise it is not preserved). You can override the parent class method at this time.

If an overloaded method is defined in a class, the system automatically chooses to invoke the appropriate method, depending on the type of the parameter, when the method is called.

Java Object-oriented

The principle of object-oriented encapsulation requires that parts outside the object not be arbitrarily accessible to the object's internal data, thus effectively avoiding the "cross-infection" of the error, making the software error localized and reducing the difficulty of troubleshooting.

Inherited

All classes inherit from Java.lang.Object, some common methods:

Equals (): No same when comparing two object references.

GetClass (): Returns the representation of the class corresponding to the object's runtime, thus obtaining the appropriate information

ToString (): Returns the object string representation

Finalize (): Used to purge objects before garbage collection

Notify (), Notifyall (), Wait (): For synchronization in multithreaded processing

Subclass (subclass) Inheritance of the parent class (superclass, superclass)

Subclasses cannot inherit member variables and methods in the parent class that have access rights private.

Subclasses can override the parent class's methods and name member variables with the same name as the parent class.

Java does not support multiple inheritance

Creating subclasses

Class Subclass extends Superclass {

...

}

Hiding of members and rewriting of methods

Subclasses can change the state and behavior of a parent class to its own state and behavior by hiding the member variables of the parent class and the methods of overriding the parent class.

    

The same method behaves differently after inheriting the parent class of a polymorphic subclass

In two ways: static polymorphism implemented by method overloading (compile-time polymorphism), method overrides for dynamic polymorphism (runtime polymorphism)

Overriding the invocation principle of a method: subclasses override methods of the parent class, call the subclass method, and conversely, call the parent class's method

An object can refer to an instance of a subclass to invoke a method of the child class

Eg:b inherits A,a's object A refers to the instance of B, and calls the method of B CallMe ()

1Import java.io.*;2Class A {3void CallMe () {4 System.out.println ("Inside A ' s CallMe ()) method");5}6}78Class BExtends A { 9 void CallMe () {10 System.out.println ("Inside B ' s CallMe () method"); 11} 12} 13 14 public class Dispatch {15 public static void Main (String args[]) {16 a = new B (); //17 A.callme (); 18} 19}             

class implementation class declaration [Public][abstract|final] class ClassName [extends Superclassname] [implements Interfacenamelist] {} repair Modifier public, abstract, final description class's properties classname for the class Superclassname the name of the parent class interfacenamelist the list of interfaces implemented for the class

Class Body class ClassName {[public | protected | private] [static] [final] [transient] [volatile] type Variablenam E         member variables [public | protected | private] [static] [final | abstract] [native] [synchronized] returntype methodName ( [Paramlist]) [Throws ExceptionList] {statements}; Member Method}

Member variable [public | protected | private] [static] [final] [transient] [volatile] type variableName; Member variable static: Static variable (class variable) Final: constant transient: Transient variable for object archive volatile: Shared variable for concurrent thread sharing

Member Methods [public | protected | private] [static] [final | abstract] [native] [synchronized] returntype methodName ([pa Ramlist]) [throws ExceptionList] {statements}; Member method Static: A class method that can be called directly by the class name abstract: An abstraction method without a method body final: The method cannot be overridden native: Integrate code in other languages synchronize D: Control access to multiple concurrent threads

  

Qualifiers in the Java class:

Private: A member of a class that is limited to private and can only be accessed by the class itself. If the constructor method is private, the class cannot be instantiated by another class.

Default: Without any access rights, it can be accessed by the class itself and the class in the same package.

Protected: A member of a class that is qualified as a protected can be accessed by the class itself, its subclasses, and other classes in the same package.

Public: A member of a class that is qualified as public and can be accessed by all classes.

    

The final keyword can modify the class, the member variables of the class, and the member methods, but with different effects

Modifier member Variable: called constant, must give the initial value

Modify Member Method: The method cannot be overridden by a quilt class

Modifier class: class cannot be inherited

  

Super: Accessing members of the parent class

Accesses a member variable that is hidden by the parent class, such as super.variable;

Call the overridden method in the parent class, such as Super. Method ([paramlist]);

Call the constructor of the parent class, such as super ([Paramlist]);

eg

1Import java.io.*;2Class Superclass {3int x;45 Superclass () {6 x = 3;7 System.out.println ("in superclass:x =" + x);8}910void DoSomething () {System.out.println ("in Superclass.dosomething ()");12}13}1415Class SubclassExtends Superclass {16int x;17Subclass () {19Super ();x = 5;System.out.println ("in subclass:x =" + x);22}2324void DoSomething () {25Super.dosomething ();26 System.out.println ("in Subclass.dosomething ()"); 27 System.out.println ("super.x =" + super.x + "sub.x =" + X ); 28} 29} 30 31 public class inhereritance {32 33 public static Span style= "color: #0000ff;" >void Main (String chars[]) {34 Subclass sc = new Subclass ( ); 35 sc.dosomething (); 36} 37}       

     

    

Simple data: Value type Composite data: Reference type

1Import java.io.*;2PublicClass Passtest {3float Ptvalue;45PublicStaticvoid Main (String args[]) {6int Val;7 Passtest pt =New Passtest ();8 val = 11;9 System.out.println ("Original int Value is:" +val);Ten Pt.changeint (Val);One System.out.println ("Int Value after Change is:" +val);Pt.ptvalue = 101f;System.out.println ("Original Ptvalue is:" +pt.ptvalue);Pt.changeobjectvalue (PT);//15 System.out.println ("Ptvalue After change is:" +pt.ptvalue); 16 17}18 19 public void Changeint ( int value) {20 value = 55; 21} 22 23 public void Changeobjectvalue (passtest ref) {24 ref.ptvalue = 99f; }26}           

When a simple data type is passed as a parameter, it is passed for the value, and when the composite data type is passed as a parameter, the address is passed

The implementation of the method body method. Local variables in the method body will mask member variables if they have the same name as member variables.

Java Object-Oriented 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: 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.