Java Fundamentals Three [deep polymorphic, Interface and polymorphic] (read head first Java Records)

Source: Internet
Author: User
Tags modifier unique id

abstract classes and abstract methods1. Declarative method of abstract class, preceded by abstract class keywordabstract class Canine extends animal{Public void Roam () {}}abstract class is no use, no value, no purpose except for being inherited. A method in a class can be used as the contract content of a subclass, and a contract is a commitment agreement to another programAbstract and non-abstract methods can be used in a classIf there is an abstract method in the class, the class must be identified as abstract.  2. The abstract method has no entities(because the program code in the method has no meaning.) Abstract classes need to be extend to make sense, and abstract methods must be overwritten to make sense.Public abstract void eat ();//no method body, direct semicolon endNote: The declared abstract method must be under an abstract class, and non-abstract classes are not allowed to have abstract methods. 3. Benefits of using abstract classes and abstract methodsPolymorphic , using the parent parameter as the parameter, return type, or array type of the method, so that different subclasses do not have to write many methods, the benefit of polymorphism is that all subtypes will have that method  Object classAll classes in Java are inherited from the class object, and object is not an abstract class. The 1.Object class has two purposesas a polymorphic method, many types of mechanisms can be dealt with.provides a specific code implementation of some of the methods, making it possible for any class to have methods available at execution time.  2.Object class As the ultimate object, what are the main methodsequals (object o)//object is equalgetclass ()//The class that gets the object initializedhashcode ()//Get the object's hash code, unique IDtoString ()//lists information such as the name of the class 3. Why not all parameters and return types are set to object 3.1java is a language with a strong type check, and the compiler determines which method can be called based on the reference type. If you want to call a method in a subclass that does not have a parent class, you cannot use the parent class as a reference variable. For example:Object O=new Ferry ();o.gofast ();//Illegal, this method is not in object.  3.2 The value returned by a variable defined with object is also of type object and cannot be assigned to a variable of another type. For example, when the object is loaded into the arraylist<object>, no matter what type it is, it can now be used as the object type. This way you can only use the method of object (you can, of course, use casts to change the type) 4. Convert an object to its original typeObject o =al.get (index);dog d= (dog) o;//using (dog) for castingD.roam ();If you are unsure if the object is not dog, you can use instanceof to determine the type, and if the conversion error throws an exception classcastexception:if (o instance Dog) {dog d= (dog) o;}  Interfacean interface is a class of 100% purely abstract classes (classes that cannot be initialized). To better use polymorphism requires an interface. Interfaces are equivalent to a specification, and subclasses need to implement all the methods.  1. Why should I have an interface? A. Some classes are not suitable for initialization, such as animal, and his subclasses can be instantiated, such as dog. So to prevent this class from being initialized (by new), the tag class is implemented as an abstract class. An abstract class cannot be instantiated, but can be used as a reference type. B. You can solve the problem of Java not multiple inheritance, the solution is to set all the methods are abstract, so that the subclass will all the methods to rewrite it again. (I understand the interface, define an abstract contract specification, and then subclass to implement the specific method of this interface, this way the class certainly adheres to this contract specification, better used polymorphism) 2. Definition of the interface (using the keyword interface)Public interface pet{specific abstract abstraction method, no content, direct semicolon end, can not write abstract keyword is also the default abstract}For example:Interface pet{String name= "Cat";void Run ();String jump ();} Description:The a.public modifier is not required if the permission of the interface is not written to the default public abstract classB. Interfaces can inherit from other interfacesC. The method of the interface is also not required to fill in the public modifier, default public. And the method does not need to write abstract keywords, also means an abstract methodd. The constant of the interface is the public static final flag by default 3. Implementation of the interfacePublic Class Dog extends canine implements pet{specific interface implementation, it is necessary to implement the implementation of all methods under the interface, this is the contract provisions} Description:A. Using implements keywords to implement interfacesB. If you want to inherit the parent class and implement the interface, extends writes in front of implementsC. A class can implements multiple interfaces at once (separated by commas), referencing multiple interfaces with the same name variable when using the interface name. Constants to define a method if it has the same name. D. Subclasses call the method of the parent class, you can use Super.xxx ();the E.UML class diagram uses a dashed arrow to indicate an implementation of an interface (arrows pointing to an interface) or as a circle; a solid hollow enclosing arrow indicates inheritance (arrows pointing to the parent class) [But I don't know why the book uses a solid arrow to represent the inheritance]   

Java basic Three [deep polymorphic, Interface and polymorphism] (read head first Java record)

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.