Java Fundamentals 8-Object Oriented 3

Source: Internet
Author: User

I. Polymorphism of a class: a reference to a parent class object to a child class

In the same method, the parameters are of the same type, but are represented in different forms due to the different operating environments.

Example: The dog class inherits the Animal class, then it can be written as: Animal dog = new Dog ();

Two. Object transformation

Upward transformation: is an implicit conversion, such as the conversion of cats into animal classes;

Downward transformation: It is a display conversion, such as converting an animal into a cat, provided the animal points to a cat;

Three. instanceof keyword

Used to determine whether a variable is a type, the left write variable, the right write type, the return result is a Boolean type;

The object of the parent class cannot access the members that are unique to the subclass (member variables, member methods), such as the dog's child class has a wagging tail, defined as the animal class of the dog is not able to call this method, if you want to access the dog to display the conversion;

Four. Abstract class (modifier keyword abstract)

1. In a class, a method does not need to be implemented, completely to the subclass to implement, this method needs to be declared as an abstract method, curly braces need to be removed.

2. Classes that contain abstract methods must be declared as abstract classes, and abstract classes can have no abstract methods and cannot be instantiated, even without abstract methods.

3. Abstract classes cannot be instantiated (new is not available).

4. When a subclass of an abstract class does not have the ability to implement an abstract method of the parent class, it also needs to be declared as an abstract class, and the method that is not implemented is declared as an abstract method, waiting for the next subclass to be implemented.

Function: The method of using abstract class is actually defined a kind of rule, the sub class that point to implement this rule, in maintenance only need to modify point to be able.

Five. Interface Class (keyword: interface that replaced the class)

1. Interface is a special kind of abstract class, can only define methods, class can implement interface, keyword implements, interface can inherit interface, keyword extends;

2. The interface is also a reference type, for example, the student class realizes the singer class interface, can create a singer class reference to the Student class object;

3. The difference between an interface and an abstract class is that the abstract class can only be inherited, and the interface may be implemented more than one, separated by commas;

4. The method default modifier in the interface is public, the member variables defined in the interface are accessible throughout the implementation class, and the default modifier public static final;

5, the implementation of the class in the implementation of the interface, if the interface also inherits the parent class interface, also need to implement the method in the parent class interface;

6. If the method in the interface is not fully implemented in the implementation class, the class is also declared as an abstract class, that is, the general class needs to be fully implemented;

7. When implementing multiple interfaces at the same time, it is necessary to avoid the names of the methods in the interface.

Six.

Package: packet, to solve the same name of the class names phenomenon;

Import: Reference to a class reference in a different package needs to be used;

Seven. Permission modifiers

Function range

Java Fundamentals 8-Object Oriented 3

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.