• 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 Implementing Interface

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can A Abstract Class Implementing Interface can define Interface method. If not then why??

 
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

Abhinesh Kumar wrote:If not then why??


If you're asking why an abstract class doesn't have to define a method for an interface it implements - Simply put: because it's not complete.

An abstract class doesn't need to define any methods because you can't instantiate it. But any concrete class that extends the abstract class will have to implement ALL methods that remain unimplemented, whether they're part of the interface or not.

That said, the usual reason for creating an abstract class is precisely to implement some methods.

Winston
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Abhinesh Kumar wrote:If not then why??


If you're asking why an abstract class doesn't have to define a method for an interface it implements - Simply put: because it's not complete.

An abstract class doesn't need to define any methods because you can't instantiate it. But any concrete class that extends the abstract class will have to implement ALL methods that remain unimplemented, whether they're part of the interface or not.

That said, the usual reason for creating an abstract class is precisely to implement some methods.

Winston




But Abstract class can extend an Abstract Class and can Define the extended Abstract class method Am i ryt?? I have Given the example below so why not interface Implemented in Abstract Class can define the Implemented Interface method. As we know Abstract class can have abstract method and non abstract method and as i have tried it can also define abstract method of extended class.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can A Abstract Class Implementing Interface can define Interface method. If not then why??



Well, have you tried to compile the code? And?

Considering your opening post: I would have thought the compiler would throw an error, because your abstract class does not implement all the Veichel methodes.

But I just tried it:



and the output is:

This is the Car Type Veichel
The Serial No of the Car is AX987GH
The Model Of this car is SUV AVI
The price of the SUV AVI is $98000000

So, what is your problem specifically?

But anyway, I would prefer to define the Car not as an abstract class, but as an interface extending Veichel. And you do not need 'abstract' in the veichel interface.

Greetz,
Piet
 
Winston Gutkowski
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

Abhinesh Kumar wrote:But Abstract class can extend an Abstract Class and can Define the extended Abstract class method Am i ryt??


Sure, but it doesn't have to, which seemed to be what you were asking.

Any abstract class can define as many or as few methods as it likes, whether or not it implements an interface, or extends a parent class - abstract or otherwise. The only class that is obliged to implement all remaining unimplemented methods is a concrete class.

Winston
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

Can A Abstract Class Implementing Interface can define Interface method. If not then why??



Well, have you tried to compile the code? And?

Considering your opening post: I would have thought the compiler would throw an error, because your abstract class does not implement all the Veichel methodes.

But I just tried it:



and the output is:

This is the Car Type Veichel
The Serial No of the Car is AX987GH
The Model Of this car is SUV AVI
The price of the SUV AVI is $98000000

So, what is your problem specifically?

But anyway, I would prefer to define the Car not as an abstract class, but as an interface extending Veichel. And you do not need 'abstract' in the veichel interface.

Greetz,
Piet





MY Question is.. If We can define the method of the Extended Abstract class Method in Abstract Class why can't we define the method The Method of Extended Interface in abstract class?? As in abstract class we can have both abstract and no abstract method.( I have shown example in previous quotes that Abstract class can define method of extened Abstract class but we are not able to do when i am trying to define the Extended Interface method in Abstract Class.)
 
Winston Gutkowski
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

Abhinesh Kumar wrote:MY Question is.. If We can define the method of the Extended Abstract class Method in Abstract Class why can't we define the method The Method of Extended Interface in abstract class??


You CAN. In fact, Piet has proved that you can by compiling it for you. But you don't HAVE TO.

As in abstract class we can have both abstract and no abstract method.( I have shown example in previous quotes that Abstract class can define method of extened Abstract class but we are not able to do when i am trying to define the Extended Interface method in Abstract Class.)


What in your example makes you think that?

Your abstract class Car extends the interface viechel, which defines the type() method, which you have then implemented in Car. Doesn't that prove your point?

Furthermore, Car can ALSO implement Sino() if you want it to - but you are not obliged to.

And that is what we have been trying to tell you:
An abstract class is not obliged to define any methods at all; so if it implements an interface with 20 methods, it can implement 0, 1, 5, or all 20 methods of that interface. The ONLY requirement is that by the time you get to a NON-abstract class, ALL 20 methods must be implemented.

It sounds to me like you're looking for some mechanism that forces you to implement ALL the methods of an interface in an abstract class that implements it, and it simply does not exist.

Winston
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Abhinesh Kumar wrote:MY Question is.. If We can define the method of the Extended Abstract class Method in Abstract Class why can't we define the method The Method of Extended Interface in abstract class??


You CAN. In fact, Piet has proved that you can by compiling it for you. But you don't HAVE TO.

As in abstract class we can have both abstract and no abstract method.( I have shown example in previous quotes that Abstract class can define method of extened Abstract class but we are not able to do when i am trying to define the Extended Interface method in Abstract Class.)


What in your example makes you think that?

Your abstract class Car extends the interface viechel, which defines the type() method, which you have then implemented in Car. Doesn't that prove your point?

Furthermore, Car can ALSO implement Sino() if you want it to - but you are not obliged to.

And that is what we have been trying to tell you:
An abstract class is not obliged to define any methods at all; so if it implements an interface with 20 methods, it can implement 0, 1, 5, or all 20 methods of that interface. The ONLY requirement is that by the time you get to a NON-abstract class, ALL 20 methods must be implemented.

It sounds to me like you're looking for some mechanism that forces you to implement ALL the methods of an interface in an abstract class that implements it, and it simply does not exist.

Winston





No at all , I am not trying to prove anything which couldn't be done. Actually when i am trying to run my program which i have posted here before. It gives and error, which i am not able to rectify .
Error is : "Attempting to assign weaker access privilege."

Why is this so, I agree myt i am mistaking something and having problem in logic.

Thanks....
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhinesh Kumar wrote:
No at all , I am not trying to prove anything which couldn't be done. Actually when i am trying to run my program which i have posted here before. It gives and error, which i am not able to rectify .
Error is : "Attempting to assign weaker access privilege."

Why is this so, I agree myt i am mistaking something and having problem in logic.

Thanks....



It would have saved a lot of time if you had mentioned that in the first place. So now we finally have the actual question... let's work on that. What line of code is the error message referring to?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface methods are abstract by default.So there is no need to specify abstract key word.Methods in interfaces are implicitly public. However, methods in classes are package-visible by default. You cannot reduce the visibility of an overriden method in class , i.e. you can't do stuff like this
 
reply
    Bookmark Topic Watch Topic
  • New Topic