Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you've got some kind of homework regarding inheritance, which is fine, but if we just tell you the answer, you're not learning the point. Understanding how Java (and most modern languages) is object-oriented is <em>very</em> important to learn how to properly code.</p> <p>I'm simplifying this a bit, but think of it this way: any noun you can think of can be its own class. A 'class' is really at its essence just a construct that we have to help us organize our code, it's just a collection of data and computations on that data. If we didn't have classes, our code would be incredibly hard to write as it gets larger and larger, and practically impossible to separate between multiple programmers to work on simultaneously.</p> <p>Inheritance is a way for us to 'inherit' information to a class from a more general class. For example, let's say we have class <code>Car</code>. <code>Car</code> has some various data fields and perhaps our program is initially designed in a way that all of our data fits totally fine into that form. So, perhaps our main method has lots of declarations like <code>Car car9 = new Car("blue", "Ford", 2005)</code>, cool. But wait -- now something that doesn't fit precisely into the data set arrives! Let's say it's a Mustang, and it needs to have data for a variable called <code>engine</code>. That's not in our <code>Car</code> class, so we have three options: 1. Add <code>engine</code> to <code>Car</code>. Obviously, this isn't ideal; many of our entries are going to end up not using that at all, and we could run into null problems down the road if we're not careful. 2. Copy <code>Car</code>, rename it <code>Mustang</code>, and add the extra data. This solution does work, but look at all of that duplicated code! Any time we change anything in either class, we're going to have to remember to go back and change the other! 3. Inheritance. This is the solution we want. Since a Mustang "is-a" Car, we can define a new class that <em>inherits</em> its options from its parent, or it's "superclass". To do that, we use the <code>extends</code> keyword: <code>public class Mustang extends Car{</code>. Now, <code>Mustang</code> automatically can use <em>everything</em> that <code>Car</code> has simply by saying <code>super().ATTRIBUTE_OR_METHOD</code>. In addition, <code>Mustang</code> can define its own new methods, new attributes, and we will be able to create Mustang classes just like we would have created a Car: <code>Mustang m = new Mustang("blue", "Ford", 2013, "awesome engine 3000")</code>. We get all of the work that we put into the Car class, without having to duplicate any code! Awesome, right?</p> <p>As a side note, Java has a really cool concept called "polymorphism" where if you extend a class, you can still use the declaration of its superclass: <code>Car m = new Mustang("blue", "Ford", 2013, "awesome engine 3000")</code>. This can be very useful as you understand the powers and drawbacks that are involved with inheritance.</p> <p>Long story short: create a superclass that holds your 'generalized' methods and data, then use inheritance with the <code>extends</code> keyword to create more specific branches (called "subclasses") of your superclass.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload