Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Extending custom classes


Java Essential Training (2011)

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 25s
    1. The history of Java
      5m 19s
    2. Understanding the principles of Java
      8m 28s
    3. Java compilation and syntax
      8m 55s
    4. Choosing a development environment
      8m 43s
  3. 19m 52s
    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
      4m 14s
    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 41s
    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 42s
    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 31s
    1. Using the String class
      5m 44s
    2. Building strings with StringBuilder
      3m 34s
    3. Parsing string values
      3m 20s
    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 3s
    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 15s
  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

please wait ...
Watch the Online Video Course Java Essential Training (2011)
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
Android Java Eclipse
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 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 = 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.

Find answers to the most frequently asked questions about Java Essential Training (2011) .

Expand all | Collapse all
please wait ...
Q: Can I safely use Java 8 with this tutorial or do I have to be using Java 6 specifically?
A: Yes, you can use Java 8 with this course.
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.

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 Already a member? Log in

* Estimated file size

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


You have completed Java Essential Training (2011).

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts. provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now

A trusted source for knowledge.


We provide training to more than 4 million people, and our members tell us that helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.