Who is the abstract class and interface?

Source: Internet
Author: User

This article introduces the following:

•. NET basics and skills

• Nonsense Technology

 

Wang Lun:

Today's topic: Similarities and Differences between abstract classes and interfaces

Guan Yu:

Haha! Good!

Abstract class:
An abstract class declares the existence of a method rather than implementing it. This class is an abstract class. It is used to create a class that reflects certain basic behaviors and declare a method for this class, but it cannot be implemented in this class, which is consistent with the interface. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of Abstract classes provide implementation for all Abstract methods in their parent classes; otherwise, they are also Abstract classes. Instead, implement this method in the subclass. That is to say, subclass or other classes that know their behavior can implement these methods in the class.
In java, abstract classes can have no abstract methods.
If an abstract method exists in a class, the class must be defined as an abstract class.

Wang Lun:

For me:
An interface is the same as an abstract class to some extent. For example, if a declaration is provided but not implemented, the integrated class must implement all the defined interface methods. Otherwise, the interface cannot be compiled.
The above-mentioned abstract class rules in Java are basically similar to. NET.

Guan Yu:

In java:
An interface is a variant of an abstract class. All methods in the interface are implicitly abstract. Multi-inheritance can be achieved by implementing such an interface. Because all methods in the interface are abstract, no program body exists. An interface can define static final member variables. I think this is a different abstract class. The implementation of an interface is similar to that of a subclass, but this implementation class cannot inherit behavior from the interface definition. This is also different from the general class inheritance mechanism. When a class implements a special interface, it defines all methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. The instanceof operator can be used to determine whether the class of an object implements an interface.

Can a method be implemented in an abstract class? Did I make a mistake?

Wang Lun:

I spoke about the similarities. Now I want to talk about the differences:
Abstract class: it can include abstract methods or non-abstract methods. That is to say, the abstract class can have implemented methods that can provide public rule definitions.
Interface: only abstract methods without implementation can be provided. In the interface, all methods are public by default. Therefore, the interface cannot have a private modifier.
Abstract class: only individual inheritance is allowed;
Interface: can inherit more.

Guan Yu:

In fact, we should talk more about their differences.

Wang Lun:

Yes. In addition, the instanceof operator symbol you just mentioned can be used to determine the interface type or other types:
In C #, an is operator can be used to determine all types. For example:


1 interface IAnytao
2 {}
3
4 class Test
5 {
6 IAnytao tao = new Anytao ();
7if (tao is IAnytao)
8 {
9Console. Write ("tao is a instance of IAnytao .");
10}
11}
12

Guan Yu:

Intanceof and is mean one thing!

Wang Lun:

In addition, is there an as operator in C #, does Java have a corresponding one?

Guan Yu:

What is?

Wang Lun:

Similar to is, but more common and more efficient

Guan Yu:

Differences:

1. The interface can be multiple implements, and the abstract class can only be single extends

2. Only interfaces are defined. abstract classes can be defined and implemented.

3. The default field definition of the interface is public static final. The abstract class field is friend by default, which is "friend" (visible in this package)

4. An abstract class is an incomplete class and needs to be further refined. The interface is a behavior specification.

5. Interfaces do not have any basic features of inheritance. They only promise methods that can be called.

6. One class can implement multiple interfaces at a time, but only one parent class can be inherited.

7. interfaces can be used to support callback, but not inheritance. The interface used for callback only provides pointers to methods, and is a type-safe "pointer method ".

 

 

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.