Java Fundamentals-One of four elements abstract (interface)

Source: Internet
Author: User

The abstract concept is the abstraction of common attributes: member variables and methods

Definition abstractions define abstract classes and methods using the abstract keyword

Abstract class
A class that contains an abstract method, called an abstract class.

So abstract classes can have member variables and non-abstract member methods that have multiple permissions, such as private. Of course, the abstract method must have some .

abstract classes are used for single inheritance and cannot be instantiated .

Inheriting classes must implement abstract methods, because abstract methods are not implemented in abstract classes, and access permissions can only be public. Instead of abstract methods, you can give the default behavior of a method, and access permissions can be multiple, but you need to consider whether the non-abstract method needs to be accessed by an inherited class.

Interface interface
interface, which is used for multiple inheritance and cannot be instantiated. Only static final member variables can be included, although member variables are not generally defined in interface. The Member method can only be an abstract method in the interface, and access permission can only be public.

Therefore, both abstract and interface, abstract methods need to be implemented in subclasses, and none of these methods can be implemented in subclasses. and the non-abstract method inside the abstract class, then the subclass can not rewrite the implementation inside the behavior

 Public classChildextendsChildrenImplementslover{ PublicChild (String name) {Super(name); }       Public voidPrintname () {System.out.println (Super. GetName ()); }       Public voidLove (String name) {System.out.println (name+ ", I love you!"); }       Public Static voidMain (string[] args) { child boy=NewChild ("Charley");          System.out.println (Boy.getname ()); Child Girl=NewChild ("Queenie");          Girl.printname ();         Boy.love (Girl.getname ());     Girl.love (Boy.getname ()); } }  Abstract classChildren {PrivateString name;  PublicChildren (String name) { This. Name =name; }      //private then child obj can not access the This method//private String GetName () {     PublicString GetName () {returnname; }      Abstract voidPrintname (); //haha () is not implemented in subclasses//abstract void haha ();}  Interfacelover{voidLove (String name);} 

The difference between Java interface and Java abstract class

   1, the abstract class can have a construction method, and the interface cannot have a construction method.   2, abstract classes can have ordinary member variables, and interfaces cannot have ordinary member variables.
 Public String pas; // error is defined in the interface

   3, abstract classes can contain non-abstract ordinary methods, and all the methods in the interface must be abstract, not a non-abstract ordinary method.   4, the access type of an abstract method in an abstract class can be public, protected, and the default type, but the abstract method in the interface is only public type, and the default is the public abstract type.   5, a static method can be included in an abstract class, and a static method cannot be contained within an interface.   6, the abstract class and interface can contain static member variables, the access type of static member variables in the abstract class can be arbitrary, but the variables defined in the interface can only be public  static type, and the default is the public static type.   7, a class can implement multiple interfaces, but can inherit only one abstract class.

Differences in Application:

Interface is more in the system framework design method to play a role in the main definition of communication between modules, and abstract classes in the implementation of code to play a role, you can implement code reuse

Java Fundamentals-One of four elements abstract (interface)

Related Article

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.