I'm new to programming and have some questions about classes.
I'll use Karel as an example:
public class Karel extends robot{
....
}
then I extend Karel:
public class SuperKarel extends Karel{
....
}
but then I want to organize some groups of methods:
public class KarelJumps extends SuperKarel {
....
}
public class KarelColor extends SuperKarel {
....
}
But then if I want to make Karel jump I have to create an instance of
KarelJumps rather the SuperKarel. But because KarelJumps is a
different object then KarelColor I couldn't use any of its methods.
So would I just put all of the KarelJumps and KarelColor methods
inside SuperKarel? Do I just make one large object?
The most useful reason for extending a class is so that you can use the underlying class's functionality in new objects, and then add behaviors and characteristics that are unique to the new type. If there are no compelling reasons for doing so, and very often there are not, then don't do it. Don't overcomplicate your design, just to try to make use of polymorphism and inheritance.
You want to use these techniques to make your applications less complex not more so. For a new programmer, I would recommend that you get the book "Code Complete" and make it your Bible for a while. You will not learn a language from it per se, but you will learn how things should be done and why that is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With