Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between abstract class and interface in java

 
Yuvaraj Deena
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ole Sandum
Ranch Hand
Posts: 65
1
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

We have a FAQ page about this: Interface vs. abstract class
 
hariharan jayaprakash
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic