Super and This Keywords

Start course
Overview
Difficulty
Beginner
Duration
2h 28m
Students
16
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll talk about the "super" and "this" keywords in Java. The super keyword is used to access the superclass members from the subclass. By using the super keyword, simply we can access constructors, fields and methods of the superclass. In our example on the left side of this slide, subclass Car is using the super keyword to access the stop method of the superclass Vehicle. The this keyword is used to access the same class members inside the same subclass. In our example on the right side of this slide, subclass Car is using the this keyword to access its own start method. Also, we can access other members of the class such as variables and constructors by using it in a similar way. 

Let's make some examples by using super and this keyword. In the object-oriented programming project, right click on the source folder and select new class. I'll specify the package name as superthis and class name as Vehicle. Vehicle class is a superclass. Let's declare three instance variables in the Vehicle class. I will declare two variables named type and model with string type. String model, type; and variable maxSpeed with int type, int maxSpeed;. Let's declare a public constructor named Vehicle and it will take three parameters: type, model, and maxSpeed. In the constructor method, I will assign parameter variable type to variable type of this class. So, I write this.type = type; The this keyword represents the Vehicle class and the type is the variables of the Vehicle class. But the type we wrote after the equal sign is the parameter of the constructor, so its value will come outside of this class when we can create an object from the Vehicle class. Okay, so as a result, the this keyword indicates the class it is in. Now let's continue. Secondly, I'll assign the parameter variable model to the variable model of this class. And lastly, we assign the parameter variable maxSpeed to the variable maxSpeed of this class. 

We use the this keyword to access the same class variables. Let's generate public getter methods of these variables to access from the main method. We'll touch on the getter and setter methods in detail in the encapsulation lecture. For now, you can do the same thing with me. In order to generate getter methods, we right click on the empty file and select the source and generate getters and setters options. I will choose the get methods of all variables and then click on the 'Generate' button. Thus, all selected getters methods will be generated automatically and I will save the code. Okay, let's create a new subclass. I will right click on the superthis package and select new class. Specify the class name as Car and click 'Finish'. Our subclass Car will extend superclass Vehicle. This means that subclass Car inherits all methods and variables from the superclass Vehicle. But if you notice, this time we got a compilation error. If you place the mouse pointer onto the Car class, Eclipse shows a quick fix dialog. It says implicit super constructor Vehicle() is undefined for default constructor. Must define an explicit constructor. 

If you remember in the inheritance lectures, we said that the subclass cannot inherit the constructor of the superclass but the constructor of the superclass can be invoked in the subclass. Okay, for this, if the superclass has a constructor with a parameter, you should add it to the subclass or you have to create an empty constructor in the superclass. The empty constructor is the parameter list constructor. So, we defined a constructor with parameters in the superclass Vehicle. So, we must define an explicit constructor in the subclass Car and we must invoke the superclass Constructor from this constructor by using the super keyword. Now, let's add it. We can add the constructor by selecting add constructor in the quick fix dialog of Eclipse. Eclipse automatically added a constructor with the super keyword. 

Notice that after the keyword super, there is a parenthesis and in the parenthesis, there are parameters in the Vehicle class. This is called a super call. Let me add this as a comment, calling the constructor of the superclass. In other words, when we try to create an object from the Car class, it will first ask us for the parameters here. Then when we enter the required parameter values, it will assign these parameter values to the relevant properties of the superclass specified here. Thanks to the getter method in the Vehicle class, we will be able to take the assigned values and use them. Okay, let's continue now. Now, I will declare a variable isAutomatic with boolean type in the class Car and its initial value can be false. Also, I'll add the variable isAutomatic to the constructor of the Car class as the fourth parameter, and I'll assign the parameter variable isAutomatic to the variable isAutomatic of this class. this.isAutomatic = isAutomatic; 

Also, I'll need to generate the getter method of the variable isAutomatic to access it from the main method. We generate the getter method of this variable by using the same way. And lastly, I will save the code. Now, let's create a test class named SuperThisTest in the same package and I will check the checkbox for the main method and click the 'Finish' button. In the main method, I will create a Car object. Car myCar = new Car(), and as you can see it's waiting for us to enter parameters, so I will enter them. The first parameter Car represents the type of car. The second parameter Ferrari represents the model of the car. The third parameter 320 represents the max speed of the car. And last parameter true represents the isAutomatic of the car. We print the properties of the Car class by using the print methods. First, I'll display the type of car. System.out.println("type: " +) and after the plus sign, I will invoke the get type method from the superclass Vehicle. 

So, I write myCar.getType()); Thus, thanks to the getType method, we can get the value of car which is the value parsed to the type variable in the Vehicle class. Now, let's get the other values. In the second print method, I will display the model of the car and I will invoke the get model method from superclass Vehicle. In the third print method, I'll display the max speed of the car and I'll invoke the getMaxSpeed method from superclass Vehicle. In the last print method, I will display is car automatic or not, but this time I will invoke the isAutomatic method from subclass Car because we defined the isAutomatic variable and its getter method in the Car class. Okay, let's run the code. You see the results in the console. Type is Car, model is Ferrari, and maxSpeed is 320. These properties come from superclass Vehicle, but Car, isAutomatic, true comes from subclass Car. The thing to keep in mind is that with the keyword super, you point to a superclass but with the this keyword, you point to the class you are in. Yes, let's take a short break here, we'll continue in the next video. See you in the next video.

 

About the Author
Students
75
Courses
18
Learning Paths
1

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics