Introduction To Inheritance

Infinite Skills
A free video tutorial from Infinite Skills
High Quality Training
4.3 instructor rating • 323 courses • 1,266,907 students

Learn more from the full course

Programming Java for Beginners - The Ultimate Java Tutorial

Learn Java Programming at your own Pace. Comes Complete with Working Files and a Verifiable Certificate of Completion.

09:44:48 of on-demand video • Updated January 2017

  • Learn Java Programming from a professional trainer from your own desk.
  • 112 lectures (9.75 hours of content) teaching you object-oriented programming, classes, objects, loops, and much more!
  • Suitable for beginner programmers and ideal for users who learn faster when shown.
  • Visual training method, offering users increased retention and accelerated learning.
  • Breaks even the most complex applications down into simplistic steps.
English In this chapter, we're going to introduce the concept of inheritance. Inheritance is the ability of a class to inherit, to use the word and the definition, the features of another class. For example, in this lesson, we're going to look at a class called the employee class. That will be our base class, or the primary class from which we're going to inherit. And then we're going to create a manager class which will inherit features of the employee class. We call the employee class the base class or the super class. Super class may be a better term. And the manager class will be a sub class from that super class. So, let's get started and we'll try to differentiate some of these terms as we go through the example. So, what we wanna do first is create our super class, the employee class. So go to your DOS prompt and create a new file. Called In that class. Well, first, you're gonna type public class employee. [BLANK_AUDIO] And, we're going to keep this very simple. We're not gonna develop the classes fully, so that we have time to discuss the important aspects inherent to this. So, we're going to have just a couple of data members. A name, and a salary. That will be quite enough to get started. So the constructor will consist of two parameters. S and N. N being the name and S being the salary. So, we'll simply assign those directly. Then we're gonna write some getter methods just to return the data that's stored in the class. So, we're gonna write public stream get name, to return the name. And public int salary, or good salary, I'm sorry. [BLANK_AUDIO] To return the salary. And that's all we're going to do for right now. So, save that. Bring your DOS window back up, and compile it just to get that out of the way before we forget. Okay, now we're ready to create the subclass manager. So type notepad, or whatever editor you're using Here's how we begin. We start by declaring the class public class manager and then. To indicate that it's going to inherit from the employee class we write the key word extends followed by the name of the class. Employee. Once we do that we've identified employee as the super class, and manager as the sub class. Open up the body of the class. When we are declaring the data members for a sub class, we do not have to include those data members that are part of the super class. So, we already have a name and a salary for the employee class, all we need to do now is to declare those data members which differentiate a manager from an employee. In this case, we're just going to have one. The department that they manage, like so. And again, I'm doing this just to keep this first example simple. The next step is to define the constructor for this class. And it's a little confusing. We still have to provide all of the parameters of the superclass. Along with parameters to initialize the data members of the subclass. So, we still have string in for name, int s for salary and then we're going to add string d for the department. Now here's a very tricky part. When we write a constructor for a subclass, we have to call a constructor for the superclass. And we do that using a special keyword, super, when the compiler sees the keyword super, it knows to call the constructor for the super class. So, we're gonna provide the arguments for super n for the name and s for the salary. Like so. And when that happens it's like calling a method. It's going to call the constructor for the super class, and it's going to assign n to name and s to salary. And then for the second part of the constructor. We assign d to the department. So, that's how you create a constructor for a sub class. You call the super method that will call the constructor for the super class and then you add any other code you need to initialize data numbers that are just in the sub class. Then the only other thing we need to do, since we have getters for name and salary, is we need a getter for the department. So we're gonna create that one now. We just return department, and that will suffice for this first example. So the idea here that when we create an object of the manager class, it's going to inherit all the features of the employee class. So that we have all the getters that we already defined an employee are available to manager. So let's demonstrate that now. So, we're going to create a test program. [BLANK_AUDIO] I'll write public static void main right here. So the first thing let's do is let's create an employee object. Employee e1, equals new employee. We'll call this person Brown and their salary is 30000 a year. [BLANK_AUDIO] And we'll call the getters to those [BLANK_AUDIO] like so. Now let's create a manager m1. [BLANK_AUDIO] Let's say they manage the sales department. We're gonna do the manager's getters individually, so we're gonna go m1 getName, m1 getSalary. And remember we didn't define get name or get salary within manager, we defined them within employee. But because manager extends employee or inherits from, we have access to those methods. [BLANK_AUDIO] And then finally m1.getDepartment. [BLANK_AUDIO] And that should be enough, lets save the file. [BLANK_AUDIO] Now let's run it. And there we go. So, Brown is our employee. That's his name or her name, and their salary is $30,000. Then we created Smith as a manager with the salary of $50,000 and the department that they manage is sales. So the thing I want you to get from this first lesson in inheritance is that you have a super-class, in this case the employee class. And then you can create sub-classes such as the manager class that inherit the features, including the members and the methods of the super-class. So, if you go back and look at the definition for manager, here it is right here. All manager did, let's look first at the definition for employee. So, employee has data member's name and salary, and getter methods, get names and get salary. Then we take a look at manager. Manager adds the data member department. And the method, get department the getter method. But then it inherits the methods get name and get salary. So that we call it, here and here. Even though those methods were not defined explicitly in the manager class, because we extended manager from employee, it has access to those. That's kind of the beginning aspects of how inheritance works. So, in the next lesson we're going to look a little bit deeper at inheritance and how to use it