• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Difference between abstract class and interface in java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 coderanch, there will not be any implementation in Interface.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic