Object-oriented advanced continuation 1 (abstract class, interface concept, object polymorphism)

Source: Internet
Author: User
Document directory
  • Definition and usage rules of abstract classes:
  • Definition Format of abstract classes:
  • Concept:
  • Interface Definition Format:
  • Implementation Interface
  • Inherited abstract class implementation Interface
  • Interface inheritance
  • Polymorphism)

 

Basic concepts of abstract classes:

Concept: a class that contains an abstract method is called an abstract class;

Abstract method: only declared but not implemented methods are called abstract methods. abstract keywords must be used to declare abstract methods;

Abstract keywords must be used for both abstract classes and abstract methods;

Abstract METHODS only need to be declared and not implemented;

The abstract class must be inherited by the quilt class, And the subclass (if not an abstract class) must overwrite all the abstract methods in the abstract class.

Definition Format of abstract classes:
Abstract class abstract class name {attribute; access permission return value type method name (parameter) {// normal method return value;} access permission abstract return value type method name (parameter ); // abstract method // there is no method body in the abstract method}

Run the following code:

Abstract class A {public static final string name = "China"; // defines the global constant private string country; // common private attribute public void setcountry (string country) {This. country = country;} Public String getcountry () {return country;} public abstract void print (); // define an abstract method}

It can be seen from the definition that the abstract class is an abstract method more than the ordinary class.

However, although similar to common definitions, abstract classes cannot be instantiated. abstract classes must have subclasses and must overwrite all abstract methods in abstract classes.

If the subclass is also an abstract class, it does not need to overwrite all abstract methods. It has the same inheritance relationship with common classes.

Abstract class A {public static final string name = "China"; // defines the global constant private string country; // common private attribute public void setcountry (string country) {This. country = country;} Public String getcountry () {return country;} public abstract void print (); // define an abstract method} Class B extends a // it does not cover all the abstract methods in A. error {} Class C extends a {public void print () {}} public class abstractdemo02 {public static void main (string ARGs []) {A = new A (); // error, the abstract class cannot be instantiated. B = new B (); // The error occurs. The class B does not overwrite all the Abstract METHODS C = new C () in the parent Class (); // correct }}

Conclusion:

An abstract class cannot be declared using the final keyword.

Cause: abstract classes cannot be directly instantiated. If you want to instantiate them, you must have their inherited subclasses. However, the classes declared using final are final classes and cannot be inherited, there cannot be child classes, so there is a conflict between the two.

Problem:

Can constructor be defined in abstract classes? Answer: Yes.

It will not be explained, but the code description is acceptable. You only need to remember that abstract classes only have one abstract method more than normal classes, and all others are the same. The Code is as follows:

Abstract class A {public a () {// constructor System in the abstract class. out. println ("constructor in abstract class A");} Class B extends a {public B () {// constructor In the subclass super (); system. out. println ("constructor in subclass B") ;}} public class abstractdemo03 {public static void main (string ARGs []) {B = new B (); // program output: // constructor in abstract class A // constructor in subclass B }}
Basic concepts of interfaces:

Interfaces are the most important part of Java and can be understood as a special class, which is all composed of global variables and public abstract methods.

Interface Definition Format:
Interface interface name {Global constant; abstract method ;}

Run the following code:

Interface A {public static final string name = "Demo"; // defines the global constant public abstract String say (); // The abstract method public abstract void run (); // abstract method}

However, definition is often simplified during development:

Interface A {public static final string name = "Demo"; // defines the global constant string say (); // The abstract method void run (); // The abstract method}

The above two definitions are the same and there is no difference.

Implementation Interface

Similar to an abstract class, an interface must also be used through a subclass. The subclass uses the implements keyword to implement the interface.

Implementation format:

Class subclass implements interface A, interface B ...{}

When a subclass uses an interface, it must overwrite all the abstract methods in the interface. A subclass can implement multiple interfaces.

Run the following code:

Interface A {public static final string name = "Demo"; // defines the global constant string getname (); // The abstract method void run (); // abstract method} interface B {void go (); // abstract method} Class C implements, B // C subclass implements both interfaces a and B {// override all the Abstract METHODS OF THE Implemented interfaces in the subclass Public String getname () {return "Zhang San ";} public void run () {system. out. println ("start running");} public void go () {system. out. println ("Start walking") ;}} public class interfacedemo03 {public static void main (string ARGs []) {c = new C (); system. out. println (C. getname (); C. run (); C. go (); // program output: // Zhang San // start running // start walking }}
Inherited abstract class implementation Interface

· A subclass can inherit abstract classes and implementation interfaces at the same time.

· The format is as follows:

Class subclass extends parent class implements interface A, interface B... The implementation code is as follows:

Interface A {public static final string name = "Demo"; // defines the global constant string getname (); // abstract method} interface B {void go (); // abstract method} Class D {public int age = 20;} Class C extends D implements, B // C subclass implements both interfaces a and B {// override all the Abstract METHODS OF THE Implemented interfaces in the subclass Public String getname () {return "Name: zhang San "+" Age: "+ age;} public void go () {system. out. println ("Start walking") ;}} public class interfacedemo04 {public static void main (string ARGs []) {c = new C (); system. out. println (C. getname (); C. go (); // program output: // name: Zhang San age: 20 // start walking }}

Note: in use, an abstract class can implement an interface, so the subclass of the abstract class must overwrite all the abstract methods defined in the interface and the abstract class at the same time.

Interface inheritance

An interface cannot inherit an abstract class, but can use the extends keyword to inherit multiple interfaces at the same time to implement multi-inheritance of interfaces.

Format: interface sub-interface extends parent interface A, parent interface B... {}

Interface A {public static final string name = "Demo"; // defines the global constant string getname (); // abstract method} interface B {void go (); // abstract method} Interface C extends A, B // Interface C inherits from interfaces A and B {void run ();} class D implements C // subclass implements the C interface {// override all the abstract methods of the implemented interface in the subclass Public String getname () {return "Zhang San ";} public void go () {system. out. println ("Start walking");} public void run () {system. out. println ("start running") ;}} public class interfacedemo05 {public static void main (string ARGs []) {d = new D (); system. out. println (D. getname (); D. go (); D. run (); // program output: // Zhang San // start walking // start running }}

Summary:

1. An interface is a special class that only contains global constants and abstract methods.

· Abstract methods in interfaces can be declared with abstract keywords instead of abstract methods in abstract classes.

2. A class can inherit only one parent class, but multiple interfaces can be implemented at the same time.

3. An interface can inherit multiple interfaces at the same time to implement multi-inheritance of interfaces.

4. Similar to abstract classes, interfaces must be instantiated by subclasses.

5. An abstract class can implement multiple interfaces, but an interface cannot inherit an abstract class.

Object polymorphism (polymorphism)

Polymorphism is a very important concept in object-oriented systems. There are two methods in Java:

· Method overloading and rewriting

· Object Polymorphism

Explanation: overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called overloading ). The overloaded method can change the type of the returned value.

Run the following code to implement polymorphism:

Class A {// defines the class apublic void fun1 () {// defines the fun1 () method system. out. println ("A --> Public void fun1 () {}");} public void fun2 () {This. fun1 (); // call fun1 () method }}; Class B extends a {public void fun1 () {// This method covers the system. out. println ("B --> Public void fun1 () {}");} public void fun3 () {system. out. println ("B --> Public void fun3 () {}") ;}}; Class C extends a {public void fun1 () {// This method overwrites system. out. println ("c --> Public void fun1 () {}");} public void fun5 () {system. out. println ("c --> Public void fun5 () {}") ;}}; public class poldemo04 {public static void main (string asrgs []) {fun (New B (); // pass B's instance fun (New C (); // pass B's instance} public static void fun () {. fun1 (); // call the override fun1 () method in the parent class }};

Tip:

For object polymorphism method calling, the priority is: this. show (O), super. show (O), this. show (Super) O), super. show (Super) O ). The show () method is the method that is overwritten in the subclass.

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.