Start learning with our library of video tutorials taught by experts. Get started

Java Essential Training
Illustration by
Watching:

Extending custom classes


From:

Java Essential Training

with David Gassner

Video: Extending custom classes

The goal of creating an inheritance relationship between classes is to extend or inherit functionality in the subclass. I am going to demonstrate this in my OlivePress application, working in a version of the project named extending custom In this application I've defined a custom class named Olive, it has two public class fields BLACK and GREEN. It has three public instance fields, two strings in a long, and it has a private instance field for oil, an integer.
Expand all | Collapse all
  1. 10m 8s
    1. Welcome
      1m 3s
    2. Is this course for you?
      5m 35s
    3. Using the exercise files
      3m 30s
  2. 31m 24s
    1. The history of Java
      5m 19s
    2. Java compilation and syntax
      8m 54s
    3. Understanding the principles of Java
      8m 28s
    4. Choosing a development environment
      8m 43s
  3. 19m 5s
    1. Installing Java on Windows
      6m 42s
    2. Installing Eclipse on Windows
      3m 19s
    3. Exploring Java on Mac OS X Leopard and Snow Leopard
      2m 27s
    4. Installing Java on Mac OS X Lion
      3m 27s
    5. Installing Eclipse on Mac OS X
      3m 10s
  4. 46m 10s
    1. Creating a Hello World application
      11m 7s
    2. Exploring the Eclipse IDE
      8m 55s
    3. Compiling and running from the command line
      8m 2s
    4. Passing arguments to the application
      8m 17s
    5. Using the Java API documentation
      4m 5s
    6. Memory management and garbage collection
      5m 44s
  5. 58m 57s
    1. Everything is an object
      5m 59s
    2. Declaring and initializing variables
      9m 15s
    3. Working with numbers
      8m 32s
    4. Converting numeric values
      6m 40s
    5. Understanding operators
      7m 58s
    6. Working with character values
      5m 14s
    7. Working with boolean values
      5m 13s
    8. Outputting primitive values as strings
      5m 33s
    9. Creating a simple calculator application
      4m 33s
  6. 53m 40s
    1. Writing conditional code
      5m 35s
    2. Using the switch statement
      8m 50s
    3. Repeating code blocks with loops
      7m 35s
    4. Creating reusable code with methods
      6m 31s
    5. Declaring methods with arguments
      5m 41s
    6. Overloading method names with different signatures
      5m 53s
    7. Passing arguments by reference or by value
      5m 35s
    8. Creating a more complex calculator application
      8m 0s
  7. 20m 30s
    1. Using the String class
      5m 44s
    2. Building strings with StringBuilder
      3m 34s
    3. Parsing string values
      3m 19s
    4. Working with date values
      7m 53s
  8. 20m 44s
    1. Understanding compile-time vs. runtime errors
      4m 5s
    2. Handling exceptions with try/catch
      4m 55s
    3. Throwing exceptions in methods
      2m 50s
    4. Using the debugger
      8m 54s
  9. 32m 22s
    1. Using simple arrays
      4m 47s
    2. Using two-dimensional arrays
      6m 17s
    3. Managing resizable arrays with ArrayList
      7m 14s
    4. Managing unordered data with HashMap
      6m 5s
    5. Looping through collections with iterators
      7m 59s
  10. 52m 2s
    1. Understanding encapsulation
      5m 59s
    2. Creating and instantiating custom classes
      8m 8s
    3. Organizing classes with packages
      6m 47s
    4. Creating and using instance methods
      6m 52s
    5. Storing data in instance variables
      6m 56s
    6. Using constructor methods
      5m 40s
    7. Managing instance data with getter and setter methods
      8m 26s
    8. Using class variables and Enum classes
      3m 14s
  11. 41m 15s
    1. Understanding inheritance and polymorphism
      9m 12s
    2. Extending custom classes
      9m 1s
    3. Overriding superclass methods
      3m 8s
    4. Casting subclass objects
      5m 3s
    5. Understanding interfaces and implementing classes
      4m 2s
    6. Creating your own interfaces
      4m 14s
    7. Using abstract classes and methods
      6m 35s
  12. 32m 17s
    1. Managing files with the core class library
      7m 46s
    2. Managing files with Apache Commons FileUtils
      7m 32s
    3. Reading a text file from a networked resource
      7m 52s
    4. Parsing an XML file with DOM
      9m 7s
  13. 17m 39s
    1. Creating your own JAR files
      4m 54s
    2. Understanding the classpath
      5m 2s
    3. Documenting code with Javadoc
      7m 43s
  14. 47s
    1. Goodbye
      47s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Java Essential Training
7h 17m Beginner Dec 14, 2011

Viewers: in countries Watching now:

Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.

Topics include:
  • Understanding the history and principles of Java
  • Installing Eclipse and Java
  • Compiling and running from the command line
  • Managing memory and performing garbage collection
  • Declaring and initializing variables
  • Writing conditional code
  • Building and parsing strings
  • Debugging and exception handling
  • Using simple arrays
  • Creating custom classes
  • Working with encapsulation, inheritance, and polymorphism
  • Managing files
  • Documenting code with Javadocs
Subjects:
Developer Programming Languages
Software:
Android Java Eclipse
Author:
David Gassner

Extending custom classes

The goal of creating an inheritance relationship between classes is to extend or inherit functionality in the subclass. I am going to demonstrate this in my OlivePress application, working in a version of the project named extending custom In this application I've defined a custom class named Olive, it has two public class fields BLACK and GREEN. It has three public instance fields, two strings in a long, and it has a private instance field for oil, an integer.

It has a getter and a setter for the oil value and a couple of constructor methods and one custom crush method. Now, I'm going to create subclasses for the Olive class. My goal is to create classes that inherit most of the functionality of the superclass, but then set particular values that are unique to those classes. I'll place my subclasses in the same package as the superclass olive. I'll go to the package Explorer, I'll right click on the package and I'll select New> Class.

I'll name my first subclass, Kalamata, I'll set the superclass to my olive class. When you set the superclass you have to use the entire package and class name. You can't just name the class by its name. And this would be tough to type without making mistakes. So I'm going to click the browse button and let Eclipse do some of the work for me. In the superclass selection dialog I'll type Olive and I'll choose Olive and click okay and then click finish. And here is my subclass Kalamata.

Now I'll repeat that operation and create a second subclass, I'll right-click on the package, choose New > Class and I'll name this one, Ligurian another olive variety. Once again I'll browse for the superclass selecting Olive and I'll click Finish. I now have two subclasses of the Olive class. Now when I create my olives in the main application, I'd like to be specific about what kind of olives I'm creating. Right now, I'm calling the Olive constructor method three times and passing in values for the amount of oil that that Olive will generate.

Instead I'd like to say I want to create two Kalamata's and one Ligurian. All I need to do is use the appropriate constructor methods for these classes, but I can't do it quite yet. Even though the superclass has constructor methods. Constructor methods are not literally speaking members of the class. There are special kind of syntax and so they're not inherited by subclasses, each subclass needs its own explicit constructor that matches its name.

So I will go to the Kalamata class and fix the problem there first. I'll place the cursor inside the class declaration and right-click and I'll choose Source and then I'll let Eclipse do some of the work for me by choosing this option, Generates Constructors From Superclass. I'm asked which constructors I want to implement, the one with no arguments or the one with the integer argument or both. Because of the way I am designing this application I only want my subclasses to have no arguments constructors.

So I'll deselect the version that would receive an integer argument and click OK. So here's my new constructor method. There is a single line of code here named super. The super method is a way of calling the superclass's constructor method. I'll leave that in place for the moment and I'll just remove the TODO comment. I'll save those changes and go to the Ligurian class and repeat the process. Once again I'll right-click inside the Class Declaration and choose Source generate constructors from superclass, I'll deselect the constructor that receives an integer argument and click OK, and I'll remove the TODO comment.

Now I'm ready to use the subclasses in my main application. I'll go to main.java in the main application file I'm calling the constructor method for the superclass three times. Notice that I declared a variable typed as the superclass, but when I actually construct the objects I don't have to use the superclass constructor. I can instead use the subclass constructor, the native type of the object will then be the subclass, but it can masquerade as the superclass.

This is polymorphism at work. By declaring as the superclass, I can then say I've got a whole bunch of olives, but I don't have to be specific about what types of olives, but when I construct them I can be specific and affect the behavior. So I'll go to the first constructor method call and I'll change that to Kalamata, I'll type Kal and press Ctrl+ Spacebar and the rest of the class name is filled in for me by Eclipse. Now, I'll do something similar but the second one will be an instance of the Ligurian class.

I'll type Lig and press Ctrl + Spacebar, and Eclipse finds two classes that start with that string, I'll choose Ligurian and then finally, I'll change the third one and I once again use Kalamata, typing Kal and Ctrl + Spacebar. When you type Ctrl + Spacebar, you should get new import statements up at the top of your code for the new classes. Now, I've still got problems here right now my subclasses only have no arguments constructor methods. They don't have versions of the constructor method that can receive values.

When I move the cursor over the first constructor method call, I get a list of available quick fixes. What do you want to do Eclipse is asking me to match the constructor. I'll choose the first one, remove the argument and now that constructor method call matches what I have in the class. I'll do the same thing for Ligurian, removing the argument and then for the second version of Kalamata. I'll save my changes, and it will now appear as though everything is in good shape. I'll run the application, and you'll see that after running the first series of olives I get nine units of oil, and after the second 18.

Well, why is this happening? The reason is because in the superclass, in the oil declaration there is a default value of three and I am no longer doing anything to override that value. My goal is to have each of the subclasses decide for itself. How much oil do I generate, what's my color, what's my name and so on. So I'll go over to kalamata. java and I'll make those changes. First of all, going back to the superclass these three fields are public and so I can change them from directly within the subclass, I'll say this.name = Kalamata and this.flavor equals Grassy and this.

color == Olive.BLACK. Now to set the oil value once again I'll go back to the superclass, and show you that we provided a way of setting the oil value using this version of the constructor method which is public. So go back to Kalamata, and I'll pass in a value of two. So now all Kalamata olives will have the same oil value. When I save my changes, notice the error at the bottom. It tells me that if I'm going to call the superclass's constructor, that has to go at the top before any other statements.

So I am going to move that code up holding down Alt on Windows or Option on Mac and pressing the up arrow a few times, then I'll save my changes. Now I'll copy those lines of code to the clipboard, and go over to Ligurian and I'll paste that in getting rid of the super method call with no arguments. I'll change the oil value for Ligurian to five. We'll say that that's a very big olive, I'll change the name to Ligurian, I'll change the flavor to Yummy and I'll change the color to green.

So now each of the subclasses is responsible for setting its own field values, when I run the application, all I need to know is that I am creating these instances of these subclasses, all the specifics are determined by the subclasses themselves and when I run the application, it looks like I got the same values, nine units and 18 units but it turns out that that's because of the values and the particular combination of olives that I just selected. So I'll go back to Ligurian and I'm going to change the Ligurian so that it's a really tiny olive, it only gives me one unit of oil, I'll run the application again and this time the numbers change.

So you can see how creating an inheritance relationship allows you to inherit functionality from a superclass but then set specific values in the subclass that are respected by the rest of the application. From the main application's point of view, all it needs to know is these are the types of olives, all of the details are handled in the subclass code.

There are currently no FAQs about Java Essential Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.