Java keywords Abstract

Source: Internet
Author: User
Abstract Keywords can be used to modify classes or methods.

AbstractClass can be extended (add subclass), but cannot be instantiated directly.

AbstractThe method is not implemented in the declared class, but must be rewritten in a subclass.

Example
 
PublicAbstractClass myclass {}
 
PublicAbstractString mymethod ();
Note
    • A class using the abstract method is an abstract class, and must be declared as
      abstract
      .

    • abstract classes cannot be instantiated.

    • only when the abstract class subclass implements all
      abstract
      method, the subclass of abstract class can be instantiated. This type is called a specific class to distinguish it from the abstract class.

    • If the abstract class subclass does not implement all
      abstract
      method, this subclass is also a abstract class.

    • abstract keywords cannot be applied to static , private or
      final method, because these methods cannot be overwritten, they cannot be implemented in subclass.

    • methods of the final class cannot be abstract , because the
      final class cannot have subclasses.

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&

Abstract can be used as modifiers to modify classes and methods.

1. abstract class: you cannot manually create an object (JVM can create an object of the abstract class), but you can declare the reference of the abstract type.

Public class testabstract1 {
Public static void main (string [] ARGs ){
// Animal A = new animal (); // Error

Animal A = NULL;
A = new dog ();
}
}
Abstract class animal {
}
Class dog extends animal {
}

2. abstract method: there is a definition of the square method name without implementation. (only what can be done is defined, but not how to do it)

The advantage of an abstract method is that the definition and implementation of a method can be separated.

Abstract class animal {
Public abstract void eat ();
}

Relationship between abstract classes and Abstract METHODS: classes that contain abstract methods must be abstract classes. abstract classes do not necessarily contain abstract methods.

The meaning of an abstract class is inherited. A class inherits an abstract class and must implement all the abstract methods in the abstract class. Otherwise, this class is also an abstract class.

Abstract class animal {
Public void run (){}
Public abstract void sleep ();
Public abstract void eat ();
}
Class dog extends animal {
Public void sleep (){
System. Out. println ("dog sleep .");
}
Public void eat (){
System. Out. println ("Dog eat .");
}
}
Abstract class cat extends animal {
Public void eat (){
System. Out. println ("cat eat .");
}
}

You can declare a subclass object of the parent class and use polymorphism to call the abstract method.

Public class testabstract1 {
Public static void main (string [] ARGs ){
Animal A = NULL;
A = new dog ();
A. Sleep ();
A. Eat ();
}
}

Abstract classes that have constructor methods and parent classes also follow the rule of single inheritance

Class E {}
Abstract class animal extends E {
Public animal (){
Super ();
}
}

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.