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

Abstract Class Vs Interface Class

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading KB and Java Exam Cram Prep. I know their syntax but unsure about their usgage especially they can almost be used interchangeably.

For example, I can define an abstract class as interface class as long as i have ALL abstract methods within the class definition. Am I right about this statement ?

What other differences do they have that Java have to have them both separately ?



THanks,
k
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kay,

Your first difference is rite.
But the basic difference between an interface and abstact class is in the definition.
Interface is an agrement between two layers to follow a predifined chanel of communication(I know these are complex words, but this is how a definition is..), and abstarct class is just an abstarct form of a class.

Anways, as per the implementation point of view
- your first difference is very well rite,
- the second difference is the miltiple inheritance, the way java provides multiple inheritence.


Gud luck

Regards,
Sandeep Jindal
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well what I know:

1. If you extends and abstract class, you dont have to implement all the methods
(if you implement an interface you HAVE to)

2.In abstract class you can also provide some non abstract methods(you can have some implemented)

Hepofully im right and this helps you
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not forget that a class can only extend one other class (abstract or not) whereas it can implement any number of interfaces.
 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone. I, in fact, learned something new from each of your post. It summarizes all the stuffs that I read about abstract and imterface where later confused myself

Howewver, if any of you think of something else that are related to the differences between Abstract and Interface class please go on. I might know some of them but it is always better to hear from different perspectives.

Thanks again,
K
 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following information was extracted from

web page

interface I {
void setValue(int val);
int getValue();
}



Are my following explaination exactly correct? Just wanted to test my concept on this.

Alternatives C to E are completely out of the choice because "implements" requires exact methods defined in the interface class.

But why A is wrong .. ?
Is it because what Barry has mentioned? "Do not forget that a class can only extend one other class (abstract or not) whereas "

Thanks,
k
[ September 16, 2004: Message edited by: Kay Liew ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kay Liew:
[QB]But why A is wrong .. ?

A class must "implement" and interface. (not extend it). Here attempting to extend an interface in a class causes a compile error.

A class (whether abstract or concrete) can extend another class (whether concrete or abstract) but it cannot "implement" that other class.

A class (whether abstract or concrete) can implement an interface, but it can't extend it.

An interface can extend another interface, but it can't implement it.

An interface can't extend or implement a concrete class and it can't extend or implement an abstract class.

The two interface methods in your code are implicitly public so any abstract or concrete class that implements one of the methods has to declare that method as public. (All methods in an interface are public, even if you don't specify them as public in an interface).

Also, an abstract class that implements an interface doesn't have to implement ANY of the interface methods. So an abstract class that implements only one (or both, or none) of the two methods is perfectly legal.

One more point. If an interface has something like
int a =3;

The variable a is implicitly public static and final, even though you don't need to specify these modifiers.

 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Louie van Bommel ,

I am glad that you've responded to this thread. I am englightened :roll:

k
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic