Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Björn Norén
9,569 PointsJava Inheritance Questions | Best Practise
Question: Is this good or bad practise? Any tips on improving this code?
I've realized that I learn coding best by using math examples, so I have created this method to first calculate a square and then the area of a pyramid , by inherit square-file
So, first there's the square-file named: square.java
public class square {
public double calculateSquare(double height, double width){
double area = height * width;
return area;
}
}
... and there's the pyramid-file named: pyramid.java (which extends from the square-file)
public class pyramid extends square {
public double pyramidArea (){
return 0.5;
}
public static void main (String[] args){
square squareObject = new square();
pyramid pyramidObject = new pyramid();
double aSquare = squareObject.calculateSquare(2, 7);
double aPyramid = pyramidObject.pyramidArea() * aSquare;
System.out.println(aPyramid);
}
}
So, feel free to express tips and opinions if there's something I should change or improve! Thanks! Sry for my english, my native langauge is swedish
[MOD: edited code blocks - srh]
1 Answer
Ryan Ruscett
23,309 PointsHola,
This is a tuff question to be honest. If it works, I say go with it. There are several ways in which you could utilize this. If you are calling a method throughout your program. It would make sense to make it static. This way you don't have to instantiate the object square, which just takes up space. Although, in this day and age space is almost trivial.
The idea behind inheritance is two fold. It's to enhance an object, and it's to enforce rules on an object.
For example. If I have a general car class, I know all cars have 4 wheels. So I could have a base class that sets wheels to 4. Not all cars have 4 doors tho. I could create an abstract method (without a body) that requires me to enter in the number of doors. Now, say I am writing a sports car class, and I know sports cars have 2 doors. If I inherit from the general class car, it already gives me 4 wheels so I don't need to bother with that. But it forces me to use the method or override the method for defining car doors. Now I can define 2 doors instead of 4. Same goes for if I have a family car class where a family car would have 4 doors instead of 2. I am building upon an object as I go along by inheriting it's default information.
So is inheritance the best option for this? Probably not the best, but like I said before. If it works, go for it. You could make it static as mentioned above, but I also don't see a need to make it part of it's own class. It could exist, just not evident in a simple example like you have here. You could use annotations to do the computation of any value or static inner classes to define the computation within the same class. All of which would be a valid option.
it all really depends on the scope of the program/project. How many people are working on it, how big it's going to be, how complicated etc.
Does this help at all? It's a lot I know. But designing how to code software is so depending on so many other things. It's hard to say one way is the best way ya know.
Björn Norén
9,569 PointsBjörn Norén
9,569 PointsHi, thanks for your time to answer, I appreciate it alot! And it helps, a project like the car does make more sence. I think I just needed to know that I was on the right track, thanks for letting me know that I'm not completely out of the border! :)