Java Object-oriented (bottom)

Source: Internet
Author: User

The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof by return

A Boolean value that indicates whether this object is an instance of this particular class or its subclasses.

The three main characteristics of the class: encapsulation, inheritance, polymorphism;
Encapsulation: Hide complex logic and show only one interface to the outside.
Inheritance: Subclasses inherit the properties and methods exposed by the parent class, which can reduce the amount of code.
Polymorphism: A variety of forms of things.

I. Inheritance of classes
1. Concept of inheritance
Inheritance is used to describe the relationship between things, and integration enables a relational system to be formed between all things.
Inheritance is implemented through extends, where only single inheritance is allowed, that is, a class can have only one parent class, and many kinds of co-owners have a parent class, and multiple inheritance is allowed.
   2. Overriding the parent class method
    (1) Override and Reload
Rewrite: Redefine a method in a subclass that already exists in the parent class, and the permissions of the overridden method cannot be lower than the permissions in the parent class
Overloading: In a class, some methods with the same method name, but their number of arguments or the type is different. Overriding is two classes, overloading a class
(2) Note
Private decorated methods and properties in the parent class do not inherit from the quilt class to the
  3.super keywords
(1) Concept
When a subclass overrides a parent class method, the subclass cannot access the method in the parent class, so there is a super
(2) function
Call the properties and methods of the parent class with super
Call the constructor of the parent class with super, which is equivalent to this. (Super and this are written in the first line of the construction method, must be the first row)

Two. Final keyword
A final decorated class cannot be inherited, a decorated method cannot be overridden, and a modified variable is a constant that cannot be changed

Three. polymorphic

1. Understanding
The ability to have many different manifestations or forms of the same behavior is polymorphic, person two chart walk, cat Four legs walk, centipede n leg Walk This is polymorphic, polymorphic embodiment in the method of rewriting and overloading, overloaded polymorphic, also called compile-time polymorphic, rewrite polymorphic, also called Runtime polymorphic Dog's call is: Wang Bark, And the cat's cry is: Meow meow meow. This shows the polymorphism of different animal shout methods.
2. Object Type Conversions
    * * Abstract classes and interfaces can be used as reference variable types for upward transformation
    (1) Upward transformation
     1* concept
Convert a handle object to a parent type by type
such as Animal dog=new Dog ();
   Note

Only properties and methods that are common to the parent class of the subclass can be called after transformation, and the values of the properties and methods are also subclasses.
If there is a dog called rhubarb (Dahuang) He learned a skill fly (), and he will use two legs to run it upward transformation of the dog Dahuang=new Dahuang (); Now he can't fly because there is no fly method in the parent Dog, but he is still called rhubarb, And you can walk on both legs, and all of these properties are reserved for the dog class.
       3*object class
object is the parent class of all classes, he has a ToString () method, all objects can be called directly, he uses with the output
object, such as the class name of the class to which the object belongs, and the address information of the object, or the ToString ()
Method overrides, used to put back some unique information
Significance
Reduce code Duplication
There are examples of ***********************************************

(2) Downward transformation
Converting a parent object to a subtype by type conversion
1* precautions
The premise of a downward transition is that the parent object points to a subclass object (that is, it has to be transformed upward before transitioning down)
The downward transformation can only be transformed into this class of objects (cats cannot become dogs)
     Meaning
The upward transformation is similar to a kind of fuzzy processing, and the downward transformation is the removal of fuzziness and the method of recovering the lost of upward transformation.

Four. Interface
  1. Abstract class

(1) Definition
When defining a class, it is often necessary to define methods to describe the behavior characteristics that are tiring, and sometimes these methods are not deterministic so that the abstract class is created
(2) Features
Allows you to define a method without writing a method body, a method that does not contain a method body, an abstract method, an abstract class can have either a common method or both, or it can contain no abstract methods, and an abstract class cannot create an object.

2. Interface
(1) Definition
If all the methods in an abstract class are abstract, we can use the keyword interface modifier instead of class to make it an interface

(2) Features
The 1* method is all abstract, and all methods are decorated with public abstract by default, and the member variables are all defaulted with public static final decoration, so the variables are not defined in the interface.
A. Because all interfaces are abstract methods all interface cannot create instantiated objects, you need to redefine a class and implement all the methods in the interface with the Implements keyword.
The extends interface can be used to inherit another interface.
If an abstract class implements an interface as long as it implements some of the methods in the interface, the common class needs to be fully implemented.
5* A class can implements multiple interfaces, but only one class extends
6* A class can implement both inheritance and implementation interfaces but extends must be in front of implements
  (3) Understanding of abstract classes and interfaces
The 1* interface is an abstraction of behavior
The abstract class is born to be inherited.
3*java in the real life of the specific things in the map, so the abstract class is an abstraction of a thing, including properties and methods, that is, the abstract and interface to the class is the abstraction of the class part of the abstract that the behavior, in Java inheritance is a "is not" relationship, and the implementation of the interface is "there is no" relationship. If a class inherits an abstract class, the subclass must be the kind of abstract class, and the interface implementation is the one with no or no relationship
The abstract class as a parent class for many subclasses can be considered a template. And an interface can be considered a code of conduct
5* for abstract classes, if you need to add a new method, you can directly add a specific implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface is changed, then all implementations of this interface must be modified by the corresponding class.

  3. Anonymous inner class

Not to be continued

Five. Exceptions


Six. Definition and use of packages

Java Object-oriented (bottom)

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.