JAVA object-oriented features polymorphism, java object-oriented

Source: Internet
Author: User
Tags define abstract

JAVA object-oriented features polymorphism, java object-oriented
Polymorphism refers to the multi-form inheritance of objects, which is the basis for the implementation of polymorphism. Do not forget that the child parent class must have an inheritance relationship. polymorphism: 1. Reference polymorphism 1. the parent class reference can point to the class object Animal obj1 = new Animal (); 2. the parent class reference can point to the subclass object Animal obj2 = new Dog (); but we cannot use the subclass reference to point to the parent class Object Dog obj3 = new Animal (); // Error 2. Method polymorphism when creating this class object, the method called is this class method. When a subclass object is created, the method called is the method rewritten by the subclass or the inherited method. 1. define an eat () method in the parent class Animal and output a statement (the Animal has the ability to eat). Rewrite the eat () method in the subclass Dog, output a statement (the dog eats meat). In the main function of the test class, if obj1.eat (), the method of the parent class is called. if you use obj2 to call the eat () method, the subclass method is called. 2. there is another situation, such as creating a subclass Cat that inherits the Animal class of the parent class, but the inherited eat () method is not overwritten in Cat. then, we create a subclass object in the main function of the test class, Animal obj3 = new Cat (); then call obj3.eat (); then, the result calls the method that the subclass inherits from the parent class. (output: animals have the ability to eat) 3. in the last special case, the characteristics of polymorphism cannot be used. if you add a unique public void watchDoor () method to the subclass, it contains an output statement (the dog has the ability to read the door ); therefore, in the main function of the test class (you must define the object Animal obj2 = new Dog () First, you cannot use obj2.watchDoor (), that is, you cannot use the reference of the parent class to call the subclass method.

1 public class Animal {2 public void eat () {3 System. out. println ("Animal food capabilities"); 4} 5} 6 7 public class Dog extends Animal {8 public void eat () {9 System. out. println ("dogs eat meat"); 10} 11 public static void main (String [] args) {12 Animal obj1 = new Animal (); 13 Animal obj2 = new Dog (); 14 obj1.eat (); 15 obj2.eat (); 16} 17}

 

Conversion of reference types: 1. Up-type conversion (implicit/automatic type conversion) is a conversion from small type to large type. For example: Dog dog = new Dog (); Animal animal = dog; // correct, automatic type upgrade, UP type conversion 2. downward type conversion (forced type conversion) is a conversion from large to small type (risky, overflow) such as: Dog dog1 = (Dog) animal; // downward type conversion 3. the instanceof operator solves the type of the referenced object and avoids the security problem of type conversion. For example, Dog dog = new Dog (); Animal animal = dog; Cat cat = (Cat) animal; // No error occurs during compilation (compiled by Cat type ), however, an error is reported during running because it opens up space of the Dog type, and (cannot convert the reference type) cannot convert the dog object to the Cat type, this method also affects program security. In this case, the instanceof and if statements should be used together for verification to ensure program security, such as: if (animal instanceof Cat) {// determine whether the animal class contains Cat-type elements. If the Cat-type elements are contained, convert them. The instanceof returned value is boolean.
1 if (dog instanceof cat) {2 Dog l1 = (Dog) cat; 3} else {4 System. out. println ("type conversion is not allowed! "); 5}

 

Abstract class1 syntax definition: If the abstract keyword is used before the class, the class is an abstract class. 2 application scenarios:. in some cases, a parent class only knows how its subclass should contain, but cannot accurately know how these subclasses implement these methods. The abstract class constraint subclass must have these methods, but does not focus on how the subclass is implemented. B. Abstract An abstract class from multiple classes with the same features and use this abstract class as the template of the subclass, thus avoiding the randomness of the subclass design. 3. Role: limits that certain methods must be implemented by subclass, but does not focus on implementation details. 4 usage rules:. abstract: define abstract class B. abstract: Defines abstract methods. Only declarations are required and no specific implementation is required. C. abstract classes that contain abstract methods (errors may occur if abstract keywords are not applied before a class) d. abstract classes can contain common methods or abstract methods. E. abstract classes cannot directly create objects. They can define referenced variables and point to objects of a subclass. Interface:1. an interface can be understood as a Special Class, which consists of global constants and public abstract methods. If a class is a specific implementation body, an interface defines the specifications to be observed by a group of classes, the interface does not care about the internal data of these classes or the specific implementation details of methods in these classes. It only specifies that some methods must be provided in these classes. interface Definition: Unlike the class definition, the class keyword is used instead of the class keyword. basic Syntax: [modifier] interface name [extends parent interface 1, parent interface 2 ......] {zero to multiple constant definitions... zero to multiple abstract method definitions ...} the interface is used to inherit and be implemented. The modifier is generally recommended to use public. Note: The private and protected modifier interfaces cannot be used. 3. The attribute in the constant interface is a constant, even if the public static final modifier is not added during definition, the system automatically adds the method: the method in the interface can only be an abstract method. Even if the public abstract character modifier is not added during definition, the system also Four implementations are automatically added: A class can implement one or more interfaces, and The Implemented interfaces use the implements keyword. A class in java can inherit only one parent class, which is not flexible enough. It can be supplemented by implementing multiple interfaces. A class in java can implement one or more interfaces. Syntax for inheriting the interface of the parent class: [modifier] class name extends parent class implements interface 1, interface 2 ...... {class part // If the abstract class is inherited, You need to implement the inherited abstract method // you need to implement the abstract method in the interface} If You Want To Inherit the parent class, when inheriting the parent class, you must use the interface reference to point to the object that implements the interface before implementing the interface 5, and call its method, for example, IPlay ip1 = new Psp (); ip1.playGame (); The Telphone abstract class only provides the method of making phone calls and sending text messages. It does not provide the function of playing games. How can I enable a smartphone to play games without changing the abstract method? This uses our interface to describe Psp? Psp can only play games, but cannot send text messages or make phone calls.
1 public interface IplayGame {2 public void playGame (); 3} 4 5 public abstract class Telphone {6 public abstract void call (); 7 public abstract void message (); 8} 9 10 public class SmartPhone extends Telphone implements IplayGame {11 12 @ Override13 public void call () {14 // TODO Auto-generated method stub15 System. out. println ("You can call a smartphone! "); 16} 17 18 @ Override19 public void message () {20 // TODO Auto-generated method stub21 System. out. println (" smart phones can send text messages! "); 22} 23 24 @ Override25 public void playGame () {26 // TODO Auto-generated method stub27 System. out. println (" smart phones can play games! "); 28} 29} 30 31 public class Psp implements IplayGame {32 @ Override33 public void playGame () {34 // TODO Auto-generated method stub35 System. out. println ("psp can play games! "); 36} 37} 38 39 public class Cellphone extends Telphone {40 41 @ Override42 public void call () {43 // TODO Auto-generated method stub44 System. out. println ("a regular phone can call! "); 45} 46 47 @ Override48 public void message () {49 // TODO Auto-generated method stub50 System. out. println (" normal cell phone can send text messages! "); 51} 52} 53 54 public class Ceshi {55 public static void main (String [] args) {56 IplayGame smp = new SmartPhone (); 57 smp. playGame (); 58 Telphone t1 = new SmartPhone (); 59 t1.call (); 60 t1.message (); 61 IplayGame ip = new Psp (); 62 ip. playGame (); 63} 64}
When using the View Code 6 interface, it is often used with an anonymous internal class, namely an internal class without a name. It is mostly used to focus on implementation without paying attention to the name of the implementation class.
Public class Nmnbl {IplayGame ip1 = new IplayGame () {public void playGame () {System. out. println ("I am an anonymous internal class implementation interface! ") ;};}; Public static void main (String [] args) {new IplayGame () {@ Override public void playGame () {// TODO Auto-generated method stub System. out. println ("Use the second anonymous internal class to implement the interface! ") ;}}. PlayGame ();}}
View Code when naming an interface, we add I to the first letter to distinguish between common classes and interfaces.

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.