Abstract class (i)

Source: Internet
Author: User

Abstract is one of the most important features of object-oriented programming. In Java, there are two ways to embody the abstraction of OOP: interfaces and abstract classes. There are too many similarities, and there are too many different places. Many people think they can be used interchangeably when they are beginners, but not in practice. Today we will learn about interfaces and abstract classes in Java. The following is the directory outline for this article:

I. Abstract class

Two. Interface

Three. The difference between abstract classes and interfaces

Abstract class

Before you learn about abstract classes, take a look at the abstract methods. An abstract method is a special method: it is only declared, and there is no specific implementation. The declarative format for an abstract method is:

1 abstractvoidfun();

Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, it is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because abstract classes contain methods that are not specifically implemented, you cannot create objects with abstract classes.

Here's a question: In the Java programming idea, the abstract class is defined as "the class containing the abstract method", but later it is discovered that if a class does not contain an abstract method, it is also an abstract class if it is simply decorated with an abstraction. That is, abstract classes do not necessarily have to contain abstract methods. Personally think this belongs to the problem of the dead, because if an abstract class does not contain any abstract method, why design as abstract class? So remember this concept for a while, without having to delve into why.

123 [publicabstractclass ClassName {    abstract voidfun();}

As can be seen here, abstract classes exist for inheritance, if you define an abstract class, but do not inherit it, it is tantamount to creating this abstract class in vain, because you can not use it to do anything. For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) Abstract classes cannot be used to create objects;

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

In other respects, there is no difference between an abstract class and an ordinary class.

  

The following small program in-depth understanding of the abstract class

So in class animal only need to define this enjoy () method, use the abstract keyword to define the enjoy () method as an abstract method, defined as follows: Public abstract void enjoy ();

In a sense, an abstract method is used to rewrite, so the abstract method declared in the parent class must be overridden in the subclass. If you really do not want to rewrite this method in the subclass, then you can again in the subclass to define the method as an abstract method, because the sub-class think I go to implement is not appropriate, should let inherit my subclass to achieve more appropriate, so you can also inherit the subclass of the next subclass inside the parent class declaration of the abstract method, This is possible.

There is a rule here: since the method inside the parent class is abstract, it has a method that is not implemented for the whole class, and this method does not know how to implement it, then the class is incomplete, so this class should be defined as an abstract class. so the declaration of the preceding declaration class Animal should be preceded by the class of abstract, that is declared as follows: abstract class Animal, so that the Animal class becomes an abstract class. The final definition code for the animal class is as follows:

1/** 2  * Parent class Animal 3  * Add abstract to the front of class, declare it like this: abstract class Animal 4  * So Animal class becomes an abstract class 5 */  6 Abst Ract class Animal {7  8 public     String name, 9 public     Animal (string name) {One         this.name = name;12     }1 3     /**15      * Abstract Method      * There is only the definition of the method, there is no implementation of the method.      */18 public     abstract void enjoy ();     20}

  The Java language states that when a class has an abstract method, the class must be declared as an abstract class.

when a subclass inherits the parent class, if there is an abstract method inside the parent class, and the subclass feels that it can implement all the abstract methods of the parent class, the subclass must implement all the abstract methods of the parent class, such as:

1/** 2  * Subclass Dog Inherits abstract class Animal and implements an abstract method enjoy 3  * @author gacl 4  * 5 */  6 class Dog extends Animal {7     /** 8      * The Dog class adds its own unique properties 9      */10 public     String furcolor;11     (string n, string c) {a         super (n);// Calling parent class Animal construction method         This.furcolor = c;15     }16     @Override18 public     Void Enjoy () {         System.out.println ("Dog barking ...");     }21 22}

  The abstract method within the parent class, if the subclass is not realized, then declare the subclass as an abstract class , such as:

1/** 2  * The subclass cat here inherits from the abstract class animal and naturally inherits the abstract method declared within the animal class enjoy (), 3  * But the subclass cat feels it's not appropriate to implement this enjoy () method, So it declares itself as an abstract class, 4  * Then, who is going to implement this abstract enjoy method, who inherits the subclass, then who is going to implement this abstract method enjoy (). 5  * @author gacl 6  * 7 */  8 abstract class Cat extends Animal {9     /**11      * Cat Add your own unique properties      */13 public     String eyecolor;14     The public Cat (string n, string c) {+         super (n);//constructor method         for calling parent class animal This.eyecolor = c;18     }19}

The subclass cat here inherits from the abstract class animal, and naturally inherits the abstract method enjoy () declared inside the animal class, but the subclass cat feels it inappropriate to implement the enjoy () method, so it declares itself as an abstract class, then, Who is going to implement this abstract enjoy method, who inherits the subclass, who goes to implement this abstract method enjoy (). Such as:

1/** 2  * Subclass BlueCat inherits the abstract class cat and implements an abstract method inherited from the parent cat enjoy 3  * @author gacl 4  * 5 */  6 class BlueCat extends Ca T {7  8 public     BlueCat (string n, string c) {9         super (n, c);     }11     /**13      * Implements an abstract method Enjoy14      */15     @Override16 public     Void Enjoy () {         System.out.println ("Blue cat called ...");     }19     20}

The complete test code is as follows:

  1 package javastudy.summary; 2 3/** 4 * Parent Class Animal 5 * with abstract in front of class, this is declared: abstract class Animal 6 * So Animal class becomes an abstract class 7 */8 Abstrac     T class Animal {9 public string name; Animal (string name) {this.name = name; 14 } 15 16/** 17 * Abstract Method 18 * There is only the definition of the method, there is no implementation of the method.  */public abstract void enjoy (); 21 22} 23 24/** 25 * The subclass cat here inherits from the abstract class animal and naturally inherits the abstract method declared within the animal class enjoy (), 26 * But the subclass cat feels it's not appropriate to implement this enjoy () method, so it Declare itself as an abstract class, 27 * Then, who is going to implement this abstract enjoy method, who inherits the subclass, then who is going to implement this abstract method enjoy ().      GACL * @author * * * * * * * * * * * * * * * * Cat extends Animal {/** * * Cat Add your own unique properties 35 */36 Public String Eyecolor; Panax Notoginseng Public Cat (string n, string c) {Animal super (n);//constructor method for calling parent class This.eyecolor = C; 41} 4 2} 43 44/** 45 * Subclass BlueCat inherits the abstract class cat and implements an abstract method inherited from the parent class cat enjoy * @author GaCl Cat {PublIC BlueCat (string n, string c) {n, c); 53} 54 55/** 56 * Implements the abstract method enjoy 57 */58 @Override the public void enjoy () {System.out.println ("Blue cat called ..."); 61} 62 63} 64 65/** 66 * Subclass Dog Inherits abstract class animal and implements abstract method enjoy * @author GaCl s Animal {/** * * The Dog class adds its own unique properties. */The public string Furcolor; Ring c) {Animal super (n);//constructor method for calling parent class This.furcolor = C; void Enjoy () {System.out.println ("dog barking ..."); * * * * * * * * * * * testabstract * * * * * * * * * * * * * @param args * * void Main (string[] args) {94 95/** 96 * After you declare the cat class as an abstract class, you can no longer instantiate the Cat class, 97 * Because the abstract class is incomplete and lacks arms Less leg, so the abstract class cannot be instantiated.   98 *///cat C = new Cat ("CatName", "Blue"), dog d = new Dog ("Dogname", "Black"); 101      D.enjoy ();//Call yourself to implement the Enjoy method 102 103 BlueCat C = new BlueCat ("Bluecatname", "Blue"); 104 C.enjoy ();//Call yourself to implement the Enjoy method 105}106}

Thanks for the original:

Http://www.cnblogs.com/dolphin0520/p/3811437.html

Http://www.cnblogs.com/xdp-gacl/p/3648398.html

Abstract class (i)

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.