Java Learning Note 4--polymorphism

Source: Internet
Author: User

Polymorphic

1. Reference polymorphism: 1) a reference to a parent class can point to an object of this class, and 2) a reference to the parent class can point to the object of the child class

public static void Main (string[] args) {Animal obj1 = new Animal ();//The object of the parent class points to this class Animal obj2 = new Dog (); A reference to the parent class can point to the object of the subclass//dog obj3 = new Animal (); You cannot use a reference from a subclass to point to the parent class}

2. Method polymorphism 1) When you create this class object, the method called is the method of this class, and 2) when you create a subclass object, the method that is called is the method that the subclass overrides or the inherited method

public class Initial {public static void main (string[] args) {//TODO auto-generated method stub Animal obj1 = N EW Animal (); The object of the parent class points to this class Animal obj2 = new Dog (); A reference to the parent class can point to the object of the subclass//dog obj3 = new Animal ();        You cannot use a reference to a subclass to point to the parent class obj1.eat (); Obj2.eat (); }}

Note: If it is a unique method in a subclass, you cannot refer to the object of the child class through the reference to the parent class.

3. Reference type Conversions

1) Up type conversion (implicit/automatic type conversion)--small type to large type

2) down type conversion (forced type conversion)--Large to small

3) instanceof operator, resolve reference object type, avoid security problem of type conversion--determine whether a type is a subtype of a type, return a bool value

Dog dog = new Dog ();  Animal Animal = dog; Automatic type promotion upward conversion dog DOG2 = (dog) animal; Down type conversion cat cat = (cat) animal;//forced type conversion cat, compiler does not error, but animal object in memory is dog type, cast to cat type will cause type mismatch--cat is not animal subtype anymore//  Use Instanceofdog dog = new Dog ();  Animal Animal = dog;  if (animal instanceof dog) {Dog dog2 = (dog) animal;  }else{System.out.println ("Unable to Dog");  } if (animal instanceof cat) {cat cat = (cat) animal;  }else{System.out.println ("Cannot perform type conversion---"); }

Abstraction in 4.Java

1) Syntax definition: using the abstract modifier

2) Scene: a) The parent class just knows the methods that the subclass contains, and cannot exactly implement the methods of the subclass--the subclasses must have those methods;b) abstract an abstract class from multiple classes with the same feature class, using abstract classes as subclass templates

3) Role: Limit certain methods that a subclass must implement, but not focus on implementation details

4) Rules of Use: A.ABSTRACTD defines abstract methods, only declarations, no implementation; B. Classes containing abstract methods are abstract classes, c. Abstract classes can contain ordinary methods, or there can be no abstract methods; d. Abstract classes cannot be created directly, you can define reference variables

Abstract class: Package com.imooc;public abstract class telephone { public abstract  void call ();  public abstract void message ();} Sub-class 1:package com.imooc;public class smartphone extends telephone { public  void call ()  {  // TODO Auto-generated method stub         system.out.println ("via voice call");  } public void message ()  {  // todo auto-generated method stub  system.out.println (" by voice message ");  }} Subclass 2:package com.imooc;public class cellphone extends telephone  { public void call ()  {  // todo auto-generated method stub   system.out.println ("via keypad phone");  } public void message ()  {  //  todo auto-generated method stub  sySTEM.OUT.PRINTLN ("SMS via Keyboard");  }} interface Functions:package com.imooc;public class initial {  Public static void main (String[] args)  {  // todo auto-generated  method stub  telephone tel = new cellphone ();   tel.call ();   tel.message ();   telephone tel2 = new smartphone ();   Tel2.message ();   tel2.call ();  }}

Interfaces in the 5.Java

1) Interface: The interface defines the specification that some classes follow, the interface does not care about the internal data of the class, does not care about the method implementation details of the class, only specifies the method that must be provided in the class.

2) Use the interface keyword, while class is used for classes. are constants or abstract methods in an interface

3) interface is used to be inherited and implemented, use public, cannot use private and protect,interface front use abstract,abstract, public static final can omit

Public Abstract interface Iplaygame {

Public static final int a = 8;

Public abstract void PlayGame (); Can't ... playGame () {};

}

Note: Methods in interfaces cannot have method bodies such as: Wrong-void GG () {} to-void GG ();

Define interface: Public abstract interface Iplaygame {public static final int a = 6; public abstract void PlayGame ();} Inherited interface: public class PSP implements Iplaygame {public void PlayGame () {//TODO auto-generated method stub System.out.pr Intln ("Features to play the game"); }}



Java Learning Note 4--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.