• 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 classes ?

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,

I have an interface which is implemented by an abstract class:

abstract class X implements Y{
method1();

}

I then have subclasses of abstract class:

class XX extends X{
public method1(){
// implemented
}

public method2(){
// new method here
}

}

When I add a new method to the subclass ... it is not recognized when I call it. I get the error - the method is undefined for the type. Is it possible to have new methods in a subclass of an abstract class?

In my code I instantiate the interface class and then call the new method:

Y myClass = new Y();
myClass.method2();

I get a compiler error when invoking method2. Any help will be much appreciated!

Regards.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meghna,
I see two problems:
Is Y a class or an interface? If it is an interface, you can't call "new Y()" because interfaces can't be extended. If it is an interface, you can't have "implements Y" because only interfaces can be implemented.

Also, please post Y for an answer to your question about method2().
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method2() is a method only known for class XX. If you need to call it, you have to declare the variable as being of class XX, or cast it to XX. You can't cast it though unless it really is an instance of XX or a subclass of XX, or you will get a runtime exception.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your thoughts on this. Our application is based on Spring framework. Since Spring is all interface based, we are using Dependency injection to inject the the subclass XX into the bean. Y is an interface, while we do not explicitly say:

Y myClass = new Y();

Spring on startup will instantiate the correctly injected class. Thats was my mistake in explaining...I guess the above is incorrect. However, my service has the below variables declared:

private Y myClass;

Now in the application-context.txt I am injecting XX into the service. Later on in the service I have :

myClass.method2(); //This gives compiler error - undefined method.

There is no problem calling method1() from the service since method1 is implemented in the abstract class. The problem happens when I call method2, so I guess my question now is:

Can you have a subclass of an abstract class define new methods not present in the abstract class definition?

Since I have talked a bit about Spring maybe this would get a better response in the Spring forum.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meghna Bhardwaj:
Can you have a subclass of an abstract class define new methods not present in the abstract class definition?


Sure you can. However, you need to have a reference (or cast) to that subclass, not of the main class.

So you would need something like this:
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meghna Bhardwaj:

...I get a compiler error when invoking method2. Any help will be much appreciated!



I could see that 'Y' is an interface as your abstract class X is implementing that. In such case, "new Y()" would NOT have let you proceed the compilation.

Assume you have correct that, you get a compiler error in invoking the method method2(), it is because the reference variable is what matters to a compiler during compilation time and NOT the actual instance type.

Since you have declared the method2() in your subclass XX, it will NOT be available to any other reference variables except its own.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response, I think I understand where I went wrong. I need to use casting in order to call method2() since it is only defined in the XX subclass. This casting was not needed when calling method1() since it was present in the interface and implemented in the abstract class.

Ok, thanks everyone.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's perfect Meghna.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I have run into another problem. I tried to call method2() using the casting like:

((XX) Y).method2();

However, I am now getting a ClassCastException telling me I cannot cast to this type. Due to my hierarchy as I describes earlier, doesnt seem like this is possible? Can anybody please advice how to proceed, is the only way out to include method2 in the abstract class and interface?

This is the casting that is suggested by eclipse, so I assume I have not made a syntax error. I would also like to metion that I am using dependency injection and I inject this service with my subclass XX.

Many thanks.
[ January 02, 2008: Message edited by: Meghna Bhardwaj ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a lot easier if you use ctrl-C and ctrl-V to copy code. It would appear you are trying to cast the class/interface Y to an XX. You can't cast a class, you can only cast an object. Did you mean ((XX)myClass).method2()?

And remember, you can only cast an object to a type it already has!

BTW: You would find your code much easier to understand without all the Xs and Ys.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your help, yes you are right I mis-typed the line of code.

We have found the problem. It appears that normally this is possible but due to Spring being in the mix, we need to re-organize the code then this will work for me. It appears all methods must be in some interface, and then implemented in the subclass before you can have the Interface type as an instance variable in your service which calls the method.

Since Spring is all interface based we need to define all methods in an interface hierarchy and then call them.

Thanks all!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleased to be able to help and that you got it all sorted out!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is really great Meghna. Thank you for posting the solutions you have found out which helped you get rid of the issue.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic