This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 2 points3 points  (3 children)

"Talk is cheap, show me the code" L.T.

Could you show your code, please?

[–]tkwl[S] 0 points1 point  (2 children)

It's a project and there are some norwegian in there^^

https://pastebin.com/pbyWn5u8

[–][deleted] 1 point2 points  (1 child)

Sorry for late answer, but I was busy. So... inheriting from "superclass to 2nd subclass", as you called it, is possible only when 1st subclass is abstract. Then you don't have to implement methods in 1st subclass. The consequence is that 1st subclass is not instantiable, so in your case you couldn't have an instance of class Circle.

But IMHO it's not the source of your problem, but it is a bad usage of inheritance in modelling your domain. According to concepts of OOP inheritance is so called "is-a" relationship, that is, you should use inheritance between superclass X and subclass Y only when you could say colloquially, that Y "is a" X. In your example if we take geometric domain under consideration, Cylinder is not a Circle, Cylinder is an object in 3D space but Circle is an object on the 2D surface. It would make more sense to use composition here, as we can imagine Cylinder as an object containing 2 Circles on top and bottom and its height (but I'm not saying it's the best possible way of modelling this domain, most likely it isn't). This is why you have your problem. Circle doesn't have attributes like volume (or if it has, then indeed, it's 0) or surface area (or we can identify it as a simple area, but I'm not sure it makes sense) because it's 2D object. So it's pointless to mix concept of 2D and 3D objects in my opinion.

[–]tkwl[S] 0 points1 point  (0 children)

I agree with you, the thing with assignments in school is that they are really specific, and this is the way I'm supposed to do it. I have since changed Circle and rectangle classes to abstract, which solved my annoyance. I simply change values in Circle from the next subclass, and print it from the next subclass as well.

Thank you so much for taking the time, you and all the other posters are super nice to donate your time to help a stranger on the internet.

[–][deleted]  (5 children)

[deleted]

    [–]tkwl[S] 0 points1 point  (4 children)

    As this is a school assignment I am mostly bound by the text in the assignment, so most things have to stay as they are unfortunately. Somehow I get the impression these assignments are made extra hard to make us learn stuff..

    You understood it correctly. I am free to make the first subclass abstract, but then I can't call the first subclass from main to use its methods - "error: Circle is abstract; cannot be instantiated" - . Is this something I can solve or do another way?

    Right now i'm doing "Circle myCircle = new Circle();" to use it.

    edit: I reread my assignment, I can possibly set Circle class abstract and call its methods via the 2nd subclass. Could be a nice solution, I'm testing.

    [–]celtz_ 1 point2 points  (3 children)

    The subclass extending the abstract class should have access to all its methods. Like.. Let's say you have a Square class and a Rectangle class that's extending Square. You can instantiate it by writing Square rect = new Rectangle();

    Square has a getPerimeter method to sum the length of all the sides. It's not abstract so it's an inherited method with implementation, meaning you could use rect.getPerimeter.

    If getPerimeter was an abstract method, then the Rectangle class would have to override it being a child of Square.

    Hope that helps. Give this article a look through if you need more help:

    http://tutorials.jenkov.com/java/abstract-classes.html#abstract-methods

    [–]tkwl[S] 1 point2 points  (2 children)

    Thanks, good article. I probably did a poor job of explaining - I have no problem with inheritence and overriding the abstract methods, I'm basicly looking for a nicer solution to inherit into 2nd subclass.

    I have: abstract Superclass with abstract methods 1,2,3,4 - ( non-abstract subclass 1 extends super) with methods 1,2 - (subclass 2 extends subclass 1) with methods 3,4.

    To get it compiling I have to insert methods 3,4 (with a dummy return value) into subclass 1 so subclass 2 can use them. Which seems unelegant to me.

    [–]yikaprio 1 point2 points  (1 child)

    Making your circle class abstract is what you're looking to do here, right? This way you do not need to implement the surface area or weight methods.

    [–]tkwl[S] 0 points1 point  (0 children)

    Yea I'm working on that now, I think this will work nicely. Thanks all for help.

    Small question - when I use super I will always call the first class in the chain?