Home arrow static arrow Java Programming [Archive] - Abstract Class Problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Abstract Class Problem
This topic has 9 replies on 1 page.

Posts:833
Registered: 1/2/04
Abstract Class Problem  
Jun 2, 2004 8:46 AM



 
HI, I have 4 classes and the hierarchy of those classes is as follows:

DBCachedRecord

ServiceHeader

ServiceHeader22, ServiceHeader25

I made ServiceHeader abstract and ServiceHeader22 and ServiceHeader25 need to call constructors of DBCachedRecord. When i do super(..), I got the error Constructor cannot find in ServiceHeader.

There are few static methods as well in each of the class which I want to access those. And I have a method getService in ServiceHeader which decides which serviceHeader to use (either 22 or 25).

I found this really a hard one and I cannot find a solution yet. Would anybody help me in resolving this scenario.

Thanks

 

Posts:1,860
Registered: 12/14/00
Re: Abstract Class Problem  
Jun 2, 2004 8:51 AM (reply 1 of 9)



 
When i do super(..), I got the error
Constructor cannot find in ServiceHeader.

Sounds like you didn't you declare the constructor in ServiceHeader?
 

Posts:6,750
Registered: 1/25/04
Re: Abstract Class Problem  
Jun 2, 2004 8:53 AM (reply 2 of 9)



 
When i do super(..), I got the error
Constructor cannot find in ServiceHeader.

So what is your question? You believe the compiler, right? You need to either add the constructor you want to call, or call a constructor that already exists. Keep in mind the default constructor thing - if you don't know what I'm talking about then that may be the problem.

There are few static methods as well in each of the
class which I want to access those. And I have a
method getService in ServiceHeader which decides which
serviceHeader to use (either 22 or 25).

Generally having a superclass know anything about its subclasses is a bad design. Consider making these methods non-static and overriding them in the subclasses. IMO this is OK to do even if they don't use non-static data, in order to get the appropriate class-specific behavior. Others may disagree though, I don't know.
 

Posts:833
Registered: 1/2/04
Re: Abstract Class Problem  
Jun 2, 2004 8:58 AM (reply 3 of 9)



 
Because my class is abstract, declaring the custructor in that class is no use, because it never be instantiated.

Even though if I define it in the abstract class then I am getting an ClassFormatException (Code attribute is absent in method that is not abstract or native).

Does anyone suggest me a good design for this.
 

Posts:1,860
Registered: 12/14/00
Re: Abstract Class Problem  
Jun 2, 2004 9:04 AM (reply 4 of 9)



 
Because my class is abstract, declaring the custructor
in that class is no use

No, that is not the case - the compiler isn't pointing out the problem just for a laugh - its doing it to help you figure out what's wrong. You need to read up on abstract classes.

Even though if I define it in the abstract class then
I am getting an ClassFormatException

Never heard of one of those? Do you mean a ClassFormatError? If so then recompile all your classes.
When posting code and/or error messages to these forums, please always cut & paste it - if you make an error typing it in then you're just wasting our time (and your own).

(Code attribute
is absent in method that is not abstract or native).

I have no idea what you mean by this.
 

Posts:24,036
Registered: 2/3/03
Re: Abstract Class Problem  
Jun 2, 2004 9:13 AM (reply 5 of 9)



 
Because my class is abstract, declaring the custructor
in that class is no use, because it never be
instantiated.

abstract class Foo {    private String s;     public Foo(String s) {        this.s = s;    }     public static void main(String[] args) {        Foo f = new Foo("foo") {};        System.out.println(f.s); // foo    }}
 

Posts:24,036
Registered: 2/3/03
Re: Abstract Class Problem  
Jun 2, 2004 9:14 AM (reply 6 of 9)



 
P.S. That's to refute the "abstract constructor of no use" presumption.
 

Posts:3,258
Registered: 00-08-28
Re: Abstract Class Problem  
Jun 2, 2004 12:29 PM (reply 7 of 9)



 
Constructors in an abstract class can only be called from an instantiating subclass. Marking all constructors protected in an abstract class will help indicate this.
 

Posts:2
Registered: 9/1/99
Re: Abstract Class Problem  
Jul 25, 2004 8:35 PM (reply 8 of 9)



 
I just got the erro "Code attribute is absent in method that is not abstract or native" error.
The problem was that in my abstract class I had a ";" after the method definition before the code.
E.G.

public Object foo(String x); <-- notice the semicolon
{
......
return theObject;
}

Hope this helps someone. That could have been an ugly one to find.
 

Posts:2
Registered: 9/1/99
Re: Abstract Class Problem  
Jul 25, 2004 8:38 PM (reply 9 of 9)



 
jumped the gun on that one.

It was actually this

public void resetPinBlocking(Context context);

should have been

public abstract void resetPinBlocking(Context context);

missing the abstract keyword.
 
This topic has 9 replies on 1 page.