GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Difference between abstract class and interface in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between abstract class and interface in java" Watch "Difference between abstract class and interface in java" New topic
Author

Difference between abstract class and interface in java

Yuvaraj Deena
Greenhorn

Joined: Aug 24, 2013
Posts: 8
Im beginner in java programming, i just wanted to know main difference between abstract class and interface in java. However it seems both are similar.

please help me out.


Thanks,
yuva
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Yuvaraj Deena wrote:Im beginner in java programming, i just wanted to know main difference between abstract class and interface in java. However it seems both are similar.

In an abstract class, you can implement (ie, put code in) some or all of its methods if you want. With interfaces, you can't.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ole Kristian Sandum
Greenhorn

Joined: Sep 27, 2013
Posts: 15
Winston Gutkowski wrote:
Yuvaraj Deena wrote:Im beginner in java programming, i just wanted to know main difference between abstract class and interface in java. However it seems both are similar.

In an abstract class, you can implement (ie, put code in) some or all of its methods if you want. With interfaces, you can't.

Winston


When Java 8 comes out, you can make default implementations for methods in interfaces as well.

There is another significant difference between interfaces and abstract classes. And that is multiple inheritance:
A class can only extend ONE other class (includes abstract classes)
A class can implement an unlimited number of interfaces
An interface can extend an unlimited number of other interfaces
Mustafa Chechatwala
Greenhorn

Joined: May 13, 2013
Posts: 3
One more thing to add about difference between Abstract Class and an Interface is:

All the member methods declared in an Interface are Public by default and member variables are Public and Static by default. Whereas in an Abstract class, nothing is default. In fact an Interface itself is Public by default.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Welcome to the Ranch.

We have a FAQ page about this: Interface vs. abstract class


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
In Abstract Class you can have both abstract and non abstract methods, but in interface all the methods are implicitly abstract and all methods are public, there will not be any implementation in Interface.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1025
    
    5

Whilst the key differences are listed here, it is also worth pointing out when to use one or the other. In fact, a possible employer will seek for more than just the differences.

An interface is a contract which implementing classes must fulfill. The implementations can be completely different, whereas with abstract classes, there is usually some common functionality already defined which subclasses can share or override.

Look for entities/nouns to map to abstract classes e.g. Animal. Look for actions/verbs to map to interfaces e.g. Comparable, Listener.

Whilst this rule may not apply in all cases, it is a good starting point during the design phase.
 
GeeCON Prague 2014
 
subject: Difference between abstract class and interface in java