• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract Class Implementing Interface  RSS feed

 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can A Abstract Class Implementing Interface can define Interface method. If not then why??

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Piet Souris
Rancher
Posts: 2015
71
  • Mark post as helpful
  • send pies
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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....
 
Paul Clapham
Sheriff
Posts: 22690
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Parvathi Raju
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!