Java Learning (vii) abstract classes in Java

Source: Internet
Author: User

Features of abstract classes:

A: Abstract classes and abstract methods must be decorated with the abstract keyword.

B: Abstract classes do not necessarily have abstract methods, but classes of abstract methods must be defined as abstract classes

C: An abstract class cannot be instantiated because it is not specific.

An abstract class has a constructor method, but cannot be instantiated? What is the function of the construction method?

Initializes the child class to access the parent class data.

D: An abstract subclass.

If you do not want to override the method of an abstract class, the subclass is an abstract class. Rewrite all the abstract methods, this time the subclass is a concrete class

Abstract class Demo:

//Abstract class Animal//declarative format for abstract classesAbstract classAnimal {//Abstract Methods//Public abstract void Eat () {}//empty method body, this will be an error. Abstract methods cannot have principals     Public Abstract voideat ();  PublicAnimal () {}}//subclasses are abstract classesAbstract classDogextendsAnimal {}//subclasses are concrete classes, overriding abstract methodsclassCatextendsAnimal { Public voideat () {System.out.println ("Cat Eats fish"); }}classAbstractdemo { Public Static voidMain (string[] args) {//Creating Objects//animal is abstract; unable to instantiate//Animal a = new Animal (); //by polymorphic meansAnimal A =NewCat ();    A.eat (); }}

Java Learning (vii) abstract classes in Java

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.