Java-7.1 abstract classes and abstract methods

Source: Internet
Author: User

Java-7.1 abstract classes and abstract methods

In this section, let's talk about abstract classes and abstract methods.

1. What are abstract classes and abstract methods.

Add abstract before class and method. This class or method is an abstract class.

 

package com.ray.ch07;public class Test {}abstract class Instument {public abstract void Play();}

2. abstract classes and abstract methods

 

Abstract class:

(1) Not all abstract methods in abstract classes can also be implemented.

(2) abstract classes must have abstract methods.

(3) It cannot be instantiated.

Abstract method:

(1) The abstract annotation method is not implemented.

(2) An abstract method must be implemented to inherit the subclass of an abstract class.

 

3. instance comparison

The following two examples are provided to compare the differences between abstract classes and common classes.

 

package com.ray.ch07;public class Test {public void tune(Instrument instrument) {instrument.Play();}public void tune(Instrument[] instruments) {for (int i = 0; i < instruments.length; i++) {tune(instruments[i]);}}public static void main(String[] args) {Test test = new Test();Instrument instrument = new Instrument();Wind wind = new Wind();Bass bass = new Bass();Instrument[] instruments = { instrument, wind, bass };test.tune(instruments);}}class Instrument {public void Play() {System.out.println(instrument play);}}class Wind extends Instrument {@Overridepublic void Play() {System.out.println(wind play);}}class Bass extends Instrument {@Overridepublic void Play() {System.out.println(bass play);}}

In the above example, we didn't use an abstract class. here we can use new Instrument, but there will be a problem, because in most of the coding time, we do not need new Instrument classes, and the play in instrument generally does not need to be called. Therefore, the play method only needs to be available and does not need to be implemented, the new above is just an example.

 

Based on this situation, we can use abstract classes to abstract instrument and play. In this way, we can save the implementation time and realize polymorphism through the inherited and later interfaces.

 

package com.ray.ch07;public class Test {public void tune(Instrument instrument) {instrument.Play();}public void tune(Instrument[] instruments) {for (int i = 0; i < instruments.length; i++) {tune(instruments[i]);}}public static void main(String[] args) {Test test = new Test();// Instrument instrument = new Instrument();//errorWind wind = new Wind();Bass bass = new Bass();Instrument[] instruments = { wind, bass };test.tune(instruments);}}abstract class Instrument {public abstract void Play();}class Wind extends Instrument {@Overridepublic void Play() {System.out.println(wind play);}}class Bass extends Instrument {@Overridepublic void Play() {System.out.println(bass play);}}

In most cases, as we have shown above, Instrument is just an abstract class, and the play method is also abstracted, And the subclass must implement it, you can then directly use Instrument to point to these subclasses.

 

In addition, we can see from the code that Instrument cannot be new.

 

Summary: This chapter briefly discusses abstract classes and abstract methods.

 

 

 

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.