This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Difference between abstract class and interface in java  RSS feed

 
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
 
Bartender
Posts: 10575
66
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
 
Ranch Hand
Posts: 76
3
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
 
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.
 
Java Cowboy
Sheriff
Posts: 16084
88
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
 
Ranch Hand
Posts: 76
  • 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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!