Java Basics-----Interface, inheritance, polymorphism

Source: Internet
Author: User

What else is the packing class, the number class, these simple I do not want to go to the introduction, the front also probably introduced the next, inherit, polymorphic

1. Inheritance of classes

The idea of inheritance: A new subclass is developed based on an extension of a parent class. Subclasses can inherit the original properties of the parent class, or you can override the parent class's methods, or you can increase the methods that the parent class does not have, and the properties

. Use the extends keyword to identify an inheritance relationship

(

The rules for overriding methods are as follows:


1. Parameter list: must exactly match the parameter list of the overridden method.
2. Return type: Must be exactly the same as the return type or subtype declared in the overridden method in the superclass
3. Access level: must not be stronger than the overridden method, can be weaker than the overridden method.
4. Non-check exception: The override method can throw any non-checked exception, regardless of whether the overridden method declares the exception.
5. Check for exceptions: The overriding method must not throw a new check exception, or a check exception that is wider than the check exception declared by the overridden method
6. Cannot override the method marked as Final,static

Rules for overloaded methods:
1. Parameter list: The overloaded method must change the parameter list.
2. Return type: You can change the return type.
3. Modifier: modifier can be changed
4. Exception: You can declare a new or broader exception.

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Rewrite
Overriding is a representation of polymorphism between a parent class and a subclass, and overloaded overloading is a class of polymorphism
One of the manifestations. If you define a method in a subclass that has the same name and parameters as its parent, we say that the method
be rewritten (overriding). When an object of the subclass uses this method, the definition in the subclass is called, and the
The definition in the parent class is "masked", and if the subclass's method name and parameter type and number are
The parent class is the same, then the return value type of the subclass must be the same as the parent class, if more than one is defined in a class
Named methods, which either have different arguments or have different parameter types, are called overloads of the method
(overloading). The overloaded method is to change the type of the return value. In other words, the overloaded return
Value types can be the same or different.

  

1. Overloading: Method name is the same, parameter list is different . Overloading is a class in which a method with the same name is written, and the return value type of each method can be different. Be aware that the overloaded method can be distinguished by the number, type, and order of the parameter list. However, the order is not recommended for overloading, which makes the code very readable.
2. Override: Also called Overwrite, defines a method in a subclass that has the same name as the parameter list of the method in the parent class. Because subclasses inherit the methods of the parent class, overriding is redefining the method inherited from the parent class and re-filling the code in the method. overrides generally refer to the parent class and subclass, the subclass overrides a method of the parent class, of course, the method name is the same, and can not change the return value of the parent class method , such as the parent class is to return a string, subclasses rewrite this method, want to return an int, that is no, You also have to return a string.

This is a good answer, the beginner will often be asked .

  

2.Object class

  All classes in Java inherit the object class, which is the parent class for all classes.

The commonly used methods are: GetClass (); Notify ();  Wait (); cannot be overridden because it is final modified

ToString (); is to return an object in the form of a string

Equals (); Compare the actual contents of two objects = = To compare whether the references of two objects are equal

    

3. Conversion of object types

  Package tinkinginjava.chb0;

class Anima
{
//Define a method
Public static void Jiao (Anima s)//jiao method is passed Anima this type
    {
System.out.println (s);
    }
}
Public class Dog extends Anima {
    
Public static void Main (string[] args) {
        
Dog G=new Dog ();
Jiao (g);//The Sub-class passes a parameter that is the dog
//Handle class object as parent class object becomes ' up transformation '
//Because the upward transformation is a conversion from a specific class to a more abstract class, it is safe to
        
//Down transformation is to convert a more abstract class into a specific class
Anima an=new Dog ();
        
dog d= (dog) an;
        
    }

    

}

4. Abstract class

    We all know that in an object-oriented world, everything is an object, and all objects are described by classes, but not all classes are intended to describe objects. If a class does not have enough information to describe a specific object and needs other concrete classes to support it, then such a class is called an abstract class. For example New Animal (), we all know that this is an animal Animal object, but this Animal exactly what it looks like we do not know, it does not have a specific animal concept, so he is an abstract class, need a specific animal, such as dogs, cats to the specific description of it , we knew what it was like.

Abstract classes used to characterize abstract concepts cannot be instantiated in the object-oriented domain because abstract concepts have no corresponding specific concepts in the problem domain.

At the same time, abstract class embodies the idea of data abstraction and is a mechanism to realize polymorphism.

There are a few things to keep in mind when using abstract classes:

1, abstract class can not be instantiated, the work of the instantiation should be left to its subclasses to complete, it only need to have a reference.

2. Abstract methods must be overridden by subclasses.

3. As long as an abstract class containing an abstract method, the method must be defined as an abstract class, whether or not there are other methods included.

4, the abstract class can contain specific methods, of course, can not contain abstract methods.

5. Abstract methods in subclasses cannot have the same name as the abstract methods of the parent class.

6. Abstract cannot be modified in the same class as final.

7. Abstract cannot be modified with private, static, final, or native the same method. 、

4. Interface

An interface is a "class" that is more abstract than an abstract class. The quote for "class" Here is that I can't find a better word to say, but let's be clear that the interface itself is not a class, as we can see from the fact that we can't instantiate an interface. such as new Runnable (); It must be wrong, we can only new its implementation class.

An interface is used to establish a protocol between classes, which provides only a form, without a specific implementation. The implementation class that implements the interface must implement all the methods of the interface, by using the Implements keyword, he says that the class is following a particular interface or a specific set of interfaces, and that "interface is just the way it looks, but now you need to declare how it works."

There are several issues to be aware of when using the interface:

1, a interface of the party all legal access rights are automatically declared public. Only public, of course, you can display the declaration as protected, private, but the compilation will be wrong!

2, the interface can be defined as "member variables", or immutable constants, because the interface in the "member variable" will automatically become public static final. Direct access is possible through the class name: Implementclass.name.

3, there is no method of implementation in the interface.

4 . The non-abstract class that implements the interface must implement all methods of the interface. Abstract classes can be used without implementation.

5. You cannot instantiate an interface with the new operator, but you can declare an interface variable that must reference (refer to) An object of the class that implements the interface. You can use instanceof to check whether an object implements a particular interface. For example: if (anobject instanceof comparable) {}.

6, in the implementation of multi-interface must avoid the repetition of the method name.

5. The difference between abstract class and interface

1. At the syntax level, the Java language gives different definitions for abstract classes and interfaces, respectively.

Abstract classes, abstract classes can have arbitrary range of member data,

It can also have its own non-abstract method, but in the interface mode, it can only have static, cannot modify the member data (but we generally do not use the member data in the interface),

At the same time, all its methods must be abstract. In a way, an interface is a specialization of an abstract class.

In the case of a subclass, it can inherit only one abstract class (which is considered by Java for Data security), but it implements multiple interfaces.

2. Different levels of abstraction. An abstract class is an abstraction of a class, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class.

3, cross-domain different. An abstract class spans a class that has similar characteristics, whereas an interface can span different classes of domains. We know that the smoke class is discovering the public part from the subclass, and then generalizing it into an abstract class that inherits the parent class, but the interface is different.

The default method implementation

1. Abstract classes and interfaces cannot be instantiated directly, and if instantiated, the abstract class variable must point to the subclass object that implements all the abstract methods, and the interface variable must point to the class object that implements all the interface methods.

2, abstract class to Quilt class inheritance, interface to be class implementation.

3, interface can only do method declaration, abstract class can do method declaration, can also do method to achieve

4, the variables defined in the interface can only be public static constants, the variables in the abstract class are ordinary variables.

5. Abstract methods in abstract classes must all be implemented by the quilt class, if the subclass can not fully implement the parent class abstract method, then the subclass can only be abstract class. Similarly, when an interface is implemented, if the interface method cannot be fully implemented, then the class can only be an abstract class.

6, abstract method can only declare, can not be implemented, interface is the result of design, abstract class is the result of refactoring

7, abstract class can have no abstract method

8, if there is an abstract method in a class, then this class can only be abstract class

9, abstract methods to be implemented, so can not be static, nor can it be private.

10, the interface can inherit the interface, and can inherit the interface more, but the class can only inherit one root.

Java Basics-----Interface, inheritance, polymorphism

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.