What's the difference between an interface and a abstract class in Java?

Source: Internet
Author: User
Tags to use abstract class and interface in java

Original

What's the difference between an interface and a abstract class in Java?

It's best to start answering this question with a brief definition of an abstract classes and interfaces and then explore the Differences between the other.

A class must was declared abstract when it had one or more abstract methods. A method was declared abstract when it had a method heading, but no body–which means this an abstract method have no I Mplementation code inside curly braces like normal methods do.

When is the use of abstract methods in Java?

Why you would want to declare a method as abstract is the best illustrated by an example. Take a look at the code below:

/*The figure class must is declared as abstract because it contains an abstract method*/ Public Abstract classfigure{/*Because this was an abstract method the body would be blank*/     Public Abstract floatGetarea (); } Public classCircleextendsfigure{Private floatradius;  Public floatGetarea () {return(3.14 * (radius ^ 2)); }} Public classRectangleextendsfigure{Private floatlength, Width;  Public floatGetarea (Figure other) {returnLength *width; }}

Why do we declare the Getarea method to is abstract in the figure class? Well, what does the Getarea method does? It returns the area of a specific shape. But, because the figure class is ' t a specificshape (like a Circle or a Rectangle), there ' s really no definition We can give the Getarea method inside the figure class. That's why we declare the method, and the figure class to is abstract. Any classes this derive from the figure class basically have 2 options:1. The derived class must provide a definition for The Getarea method OR 2. The derived class must be declared abstract itself.

A non abstract class is called a concrete class

You should also know, the any non abstract class is called a concrete class. Knowing your terminology defintely pays off in an interview.

Now that we ' ve explored the abstract method/class concepts, let's get into the concept of interfaces and how they differ F ROM abstract classes.

Java interface versus abstract class

An interface differs from an abstract class because an interface are not a class. An interface are essentially a type that can being satisfied by any class that implements the interface.

Any class this implements an interface must satisfy 2 conditions:

    • It must has the phrase "implements interface_name" at the beginning of the class Definiton.
    • It must implement all of the method headings listed in the interface definition.
Abstract Classes and inheritance

with a interface on the other hand, the relationship between the interface itself and the class implementing the Inte Rface is not necessarily strong. For example, if we had a class called "House", which is class could also implement an interface called "airconditioning". Have air conditioning not really an essential part of a house (although some may argue this point), and the relationship is not as strong as, say, the relationship between a "townhouse" class and the "House" class or the relationship between An ' Apartment ' class that derives from a ' house ' class.1. Abstract classes is meant to beinheritedFrom, and when one classInheritsFrom another it means this there is aStrong RelationshipBetween the 2 classes. For instance, if we have a abstract base class called "canine", Any deriving classshouldBe an animal that belongs to the canine family (like a Dog or a Wolf). The reason we use the word "should" was because it is the-to-the Java developer to ensure that relationship are maintained.

Because A townhouse is a type of house, which relationship is very strong, and would being more appropriately defined through Inheritance instead of interfaces.

So, we can summarize this first point by saying, the abstract class would be more appropriate when there is a strong re Lationship between the abstract class and the classes that would derive from it. Again, the because an abstract class was very closely linked to inheritance, which implies a strong relationship. But, with interfaces there need not being a strong relationship between the interface and the classes that implement the Inte Rface.

Interfaces is a good substitute for multiple inheritance

2. Java does not allow multiple Inheritance–see the discussion Onjava multiple inheritance if you need a refresher on th Is. In Java, a class can is only derive from one class, whether it's abstract or not. However, a class can implement multiple Interfaces–which could be considered as an alternative to for multiple Inheritan Ce. So, one major difference are that a Java class can inherit from only one abstract class, but can implement multiple Interfa Ces.

Abstract classes can has some implementation code

3. An abstract class could provide some methods with Definitions–so an abstract class can has non-abstract methods with a Ctual implementation details. An abstract class can also has constructors and instance variables as well. An interface, however, can-not-provide any method definitions–it can only provide method headings. Any class this implements the interface is responsible for providing the method definition/implementation.

When to use abstract class and interface in Java

Here is some guidelines on if to use a abstract class and when to use interfaces in Java:

      • An abstract class was good if you think you'll plan on using inheritance since it provides a common base class Implementa tion to derived classes.
      • An abstract class was also good if you want to being able to declare non-public members. in a interface, all methods must ispublic.
      • If you think you'll need to add methods in the future and then an abstract class is a better choice. Because If you add new method headings to a interface, then all of the classes that already implement that interface would The changed to implement the new methods. That can is quite a hassle.
      • Interfaces is a good choice when you think that the API would not be change to a while.
      • Interfaces is also good when you want to has something similar to multiple inheritance, since can implement multiple Interfaces.

Add:

1. Variables in interface must is public, static, final

What's the difference between an, interface and an abstract class in Java?

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.