• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Abstract Class & Interface

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we should use Interface and When to use Abstract Class.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both abstract classes and Interfaces have one goal in common. They should be used when the implementation is not specific. To make things clear let us say you have a 'Shape' class. Shapes could be of different kinds for e.g. a circle, a rectangle, a square etc. If 'Shape' class had a method called 'draw()', during designing you class, you cannot specifically say how this method should be defined. That's because each shape is drawn differently. So you have a draw method for square, a rectangle etc.
Here both abstract classes and interfaces can be used. Now if you want to enforce super-class sub-class relationship between shape and square and other classes you use abstract classes. Whereas in interfaces such relationship is not enforced.
Hope it's clear now..
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General(beginner) forum..
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally an interface is implemented to gain methods while a class is extended to gain information (instance variables) and perhaps methods as well.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has been asked here many times before. Using the search tool, I found the following threads on the subject:
https://coderanch.com/t/386670/java/java/Classes-Abstract-Interface
https://coderanch.com/t/386847/java/java/Abstract-Interface
https://coderanch.com/forums/
https://coderanch.com/t/387081/java/java/difference-interface-abstract
https://coderanch.com/t/387121/java/java/Abstract-vs-Interface
https://coderanch.com/t/387137/java/java/Extends-vs-Implement
https://coderanch.com/t/387287/java/java/abstract-class-interface
And those are only from the first dozen matches out of a hundred! I'm sure one of those will answer your questions. If you still don't understand, feel free to use the search tool yourself.
Of course, we are always here to clarify any confusions you may have, so feel free to come back with more questions.
Keep coding!
Layne
 
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic