"Cicada Hall Learning Notes" Java interface and abstract class

Source: Internet
Author: User

The protagonist: abstract class, interface.

For Java beginners such as Pippi, interfaces and abstract classes can be difficult to figure out without spending a lot of effort and time, and I also feel that I am unfamiliar with these two concepts in the recent week of project learning, so that some problems can not be understood clearly, and then go to the data on the abstract class with the interface made some summary.

1. Abstract class:

  Abstract class creation, after the actual abstract method, so want to understand the abstract class must first understand the abstract method , the abstract method by the name can know it is a special method, is abstract, only the method of declaration, no concrete implementation. So you can't see its function, it's very abstract.

Abstract methods must be modified with the abstract keyword, whose declaration format is abstract void f (); So if a class contains an abstract method to call this class an abstract class, abstract classes must also be decorated with the abstract keyword.

  One thing to note here is that a class does not contain abstract methods, but is modified by the abstract keyword, and this class is also an abstraction class. Abstract classes are not only abstract methods, but also can have common methods. You can also have member variables,

  Because abstract methods inside the abstract class can not be implemented specifically, so in order to solve the problem with the inheritance. The subclass must override all the abstract methods of the parent class to be instantiated, or the subclass must be decorated with abstract.

Specific code:

 //here is the definition of an abstract class demo, and then let its subclasses Demo1 to inherit it, the following subclass method completely overrides the parent class method, so write without error. 
Package Suanfa; abstract class Demo { abstract void show (); abstract void Pi ();} class Demo1 extends Demo { void Show () {} void Pi () {}}
 //However, after I annotate the Pi method of the subclass, the following error will appear in Eclipse, which tells you that you must implement the Pi method. 

Package Suanfa; abstract class Demo { abstract void show (); abstract void Pi ();} class Demo1 extends Demo { void Show () {} // void Pi () { // }}
Of course, I will write Demo1 as abstract, and there will be no error.
PackageSuanfa;Abstract classDemo {Abstract voidShow (); Abstract voidpi ();}Abstract classDemo1extendsDemo {voidShow () {}//void Pi () {// }}

The main differences between abstract classes and ordinary classes:

①: The abstract method cannot be modified by private because the private adornment method subclass cannot be overridden.

②: Abstract classes cannot create objects.

③: If a class inherits an abstract class, it must implement all the methods inside the abstract class, and if the subclass does not implement all the methods of the abstract class, the subclass must also be decorated with the abstract keyword as an abstraction class.

2. Interface:

An interface is a declaration of a series of methods that can be understood as a collection of stored methods. Interface only the method of the characteristics, can not be specific implementation methods, so the interface inside the method is an abstract method. These methods can be implemented by different classes in different places.

Allows a class to follow multiple specific interfaces.

An interface is an extremely abstract type.

3. The difference between an interface and an abstract class

①: Abstract classes can provide implementation details of member methods, and only public abstract methods exist in an interface;

②: The members of an abstract class can be of various types, and the interface can only be public static final type;

③: There cannot be static code blocks and static methods in an interface, which can be in an abstract class.

④: A class can inherit only an abstract class (one to one), a class may implement multiple interfaces (a pair of many);

Example:

Abstract class is the abstraction of things, that is, the abstraction of the class, and the interface is abstract behavior, the action is abstract.

such as: dogs and cats have a common behavior run, then can be designed to be a dog class, the cat is designed as a cat class, but you cannot design run as a class. At this point, you can put the run into an interface, and then the dog and cat can go to run this interface, there is run () this method, and then the dog inside the puppy, big dog different types, directly inherit the class of dog. As can be seen here, the interface I only need to have to run this action of the class can implement the run this interface.

Abstract classes are template designs, and interfaces are code of conduct.

Here is an example of an online door-finding and alarm:

Doors have open () and close () These two actions, you can define an abstract class door or a door interface to place the two methods:

Abstract class door{    publicabstractvoid  open ();      Public Abstract void close ();}

Or:

Interface door{    publicabstractvoid  open ();      Public Abstract void close ();}

But in the back we need to join the alarm.

If we will alarm alarm () also put into door this class will find that all of our doors have been given the function of alarm, in fact, some of our doors can not alarm.

If we put alarm (), open (), close () these three actions into the door interface, we will find that we need to use the alarm function, the interface of the Open () and close () both methods are also implemented

So here we can see that alarm () and open (), close () are exactly two different categories of behavior, open (), close () is the intrinsic behavior of the door, alarm () is later added behavior. So the best solution is to put the two behaviors of open (), close () into the alarm class and alarm () into an interface separately. Redesign an alarm to inherit the door class and implement the alarm interface.

Abstract classdoor{ Public Abstract voidopen ();  Public Abstract voidclose ();}Interfacealarm{ Public Abstract voidAlram ();}classAlarmdoorextendsDoorImplementsalarm{ Public voidAlram () {} Public voidopen () {} Public voidclose () {}}

www.zhiliaotang.com Please pay attention to Cicada Hall, there are too many studious programmers, join us, together hi skin, learn together.

"Cicada Hall Learning Notes" Java interface and abstract class

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.