2

SEE THE EDIT HALFWAY DOWN THE POST.

I'm new to java and all the formal declarations of inheritance are getting me a little confused.

I have a interface like so:

public interface A{
    public void one();
    public void two();
}

and then I have two classes like so:

public class B implements A{
    private int num;

    public void one(){
        ...
    }
    public void two(){
        ...
    }
    public B(){
        this.num = 1;
    }
}

public class C extends B{
    public C(){
        super();
    }
}

then I have a driver class like so:

public class Driver{
    public static void main(String [] args){
        A a_array[] = new A[5];

        for(int i=0; i<6; i++){
           if(i%2==0){
               a_array[i] = new B();
           }
           else{
               a_array[i] = new C();
           }
        }
    }
}

Basically, given an array of interfaces I am trying to implement various classes that implement that interface.

Now my guess is there are several things wrong with this implementation, but I seem unable to sniff them out. Primarily right now I am getting the error 'B() is not abstract and does not implement method one()'.


EDIT:

alright lets try this... the interface:

public interface Shape{
     public double calcAread();
     public double calcPerimeter();
}

the implementing class:

public class Rectangle implements Shape{
    private double length;
    private double width;

    public double calcArea(){
         return this.length*this.width;
    }

    public double calcPerimeter(){
         return (this.length*2)+(this.width*2);
} 

    public Rectangle(double length, double width){
        this.length=length;
        this.width=width;
    }
    // then some other methods including the set methods
}

the extending class:

public class Square extends Rectangle{
    public Square(){
        super();
    }

    public Square(double sideLength){
         super.setLength(sideLength);
         super.setWidth(sideLength);
    }
    // some more methods
}

I can't think of very much more that would be useful other than to mention that there are other inheriting and extending classes off of these but they follow exactly the same design and sentax.

No errors when I compile shape, but the 'Rectangle is not abstract and does not override abstract method calcAread() in Shape' error is tripped when I compile the Rectangle class.

Hopefully this will be more enlightening.

Thanks

6
  • 1
    "Array of interfaces" would be an array that contains interfaces: Class[] interfaces = { SomeInterface.class, SomeOtherInterface.class }. You're talking about an array of A, where A is an interface. Commented Oct 7, 2011 at 4:59
  • to be clear the error I get on compile is 'B is not abstract and does not override abstract method one() in A' Commented Oct 7, 2011 at 5:14
  • There's no way you can get that error from the code above. Commented Oct 7, 2011 at 5:33
  • as stated blow this isn't my actual code, just an example of how I'm setting up the classes. I'm trying to avoid posting all 400+ lines of my program, but outside of the inheritance statements I don't really know where the problem would lie. Any suggestions on how to narrow it down? Commented Oct 7, 2011 at 5:38
  • 1
    Try reading about the SSCCE. Beyond that, all I can suggest is reading up on abstract methods. Commented Oct 7, 2011 at 5:40

2 Answers 2

3

the only problem I see in the code is that the i<5 instead if i<6. array size is 5 and the initialization is set to i=0. (loop iterations should be 0,1,2,3,4, otherwise u will get ArrayIndexOutOfBound exception)

I compiled the code and its running fine.

Sign up to request clarification or add additional context in comments.

Comments

1

What you've provided as example code will work just fine. My suspicion is that your exact code and your example code differ.

Without seeing the exact error message and B class it's hard to say, but I'm willing to bet you have either a return value or parameter difference between the definition of one in your interface and your one in your implementation.

Edit: Here's what I see as the problem. Your interface's method is called "calcAread". Is that d supposed to be on the end?

public double calcAread();

Because it's missing inside Rectangle

public double calcArea()

That's going to cause a problem. It makes me wonder how @Zohaib managed to compile it actually!

6 Comments

yep, the example code is just showing the basic flow I'm using. I don't really know what elements might be causing this error and was trying to avoid posting my entire code. Any suggestions would help.
@Daniel I have added more to my answer with an update (and yes, I slept in the mean time hehe). I believe I've found the problem. :-) You will hit yourself in the forehead when you see it, and trust me we've all been there. I spent 6 hours at work tracking down a bug because we had two structures that differed by one letter, and we were iterating over the wrong one. Ironically, we were missing a d, where you had an extra one. The only logical conclusions is YOU STOLE MY D!!! :-)
wow, this brings new meaning to embarrassing debugging. Well thanks anyways.
@Daniel don't be embarrassed. Like I said, I spent 6 hours of my companies time tracking something almost identical to this yesterday. (To be fair, I didn't put it in the code, but I still should have caught it while working on it.) Meanwhile, three analyists couldn't do testing while we're at it. This kind of thing happens to everyone. Hang in there :)
@glowcoder....actually when i posted the answer, code for only interface A, classes B, C and Driver was provided. I compiled that code by removing dots inside the function and it was working fine. remaining examples were added later on.....although one needs to have good eyes to identify this sort of bug by just looking at the code....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.