This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Abstract classes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract classes ?" Watch "Abstract classes ?" New topic
Author

Abstract classes ?

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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().


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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:
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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

Joined: Apr 20, 2006
Posts: 3344

Yes, that's perfect Meghna.
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Jun 08, 2007
Posts: 109
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
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Pleased to be able to help and that you got it all sorted out!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That is really great Meghna. Thank you for posting the solutions you have found out which helped you get rid of the issue.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Abstract classes ?
 
Similar Threads
class defined inside an interface
About Overridden II (a Mock Question question...)
abstract class method suggestion
sheer mind-searing frustration - arghh
clarification on why the modifier private changes output..