image
super vs super()
Start course
Difficulty
Beginner
Duration
2h 28m
Students
140
Ratings
5/5
starstarstarstarstar
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 lesson we'll talk about the super keyword and the super call expression. We talked about that this keyword in our previous lesson. In fact, it would not be wrong to say that the keyword super and this are logically similar to each other. The difference is that, that this keyword  refers to the class it is in while the super keyword refers to the super class it inherits. Again, the this call statement refers to the constructors it contains while the super call statement references the constructors of the super class it inherits. Therefore super is actually one of the references in Java. The super keyword in Java is a reference variable which is used to refer immediate parent class object. We can use the super keyword basically three different types, refer to the parent class instance variable, to invoke the parent class method and the super call expression can be used to invoke the parent class constructor. 

Okay, without further ado, let's look at the use of keyword super and the super call expression. First, I will create a super class name Animals, right click on the source folder in the object-oriented programming project and select new and class options. The package name can be super example. The class name can be Animals and click the finish button. Okay, the Animals class is ready. First, I will use the super keyword to access the super class instance. For this, I will create an instance in this super class Animals. Boolean isMammal = true. Okay, now, I will create the subclass. Right click on the super example package and select the new and class options. The class name can be Snakes. To create this class, I will click the finish button. This class will extend the Animals class. So, I write extend Animals here and I will create an instance variable in this class. Boolean isMammal = false; Also, I will create a print method to print the boolean values on the console. Public void printMammal(). 

First, I will print the isMammal instance variable of the subclass, Snakes. System.out.println(isMammal); Now, let's print the isMammal instance variable of the super class Animals. System.out.println(super.isMammal); So, here the super keyword references the super class Animals. So, the Animals and Snakes, both classes have a common property isMammal. If we print that isMammal property, it will print the isMammal of the current class by default. To access the parent property, we need to use the super keyword. Now, let's test it. First, I will create a new class. Right click on the super example class and select new and class options. The class name can be SuperTest. I'll check the checkbox and click the finish button. I will create an object from the Snakes class, Snakes s = new Snakes. Now, I will call the print mammal method in the Snakes class and let's run the code. 

As you can see the true and false are printed on the console. So, we used the super keyword to access the instance of the super class. Okay, now let's use the super keyword to access the methods of the super class. I'll open the Animals super class. The super keyword can also be used to invoke the methods of the super class. It should be used if the subclass contains the same method as the parent class. In other words, it's used if the method is overridden. If you remember, we talked about the method overriding in the previous lessons. Now, I will create a method in the Animals super class, public void eat() and this method will print the Animals eat on the console. Now, let's open the subclass, Snakes. In this class, I'll create three methods. The first one will be the overriding method. So, I write public void eat() and I will print the Snakes eat. 

The second method will be the own method of this class, public void bite()  and I will print the Snakes bite and the third method will be the printing of this statement on the console. Public void printSituation() First, I will call the bite method of this class. Now, let's call the eat method of the super class. For this, I will use the super keyword super.eat and lastly, let's call the eat method of this class. Okay, now the Animals and Snakes classes have the eat method. If we call the eat method from the Snakes class, it will call the eat method of the Snakes class by default. To call the parent class method, we need to use the super keyword. Now, let's test it. I opened the super test class, in the main method, I will delete the isMammal method and instead I write s.printSituation(); 

Now, let's run and test. As you can see, first the Snakes bite is printed, then the Animals eat is printed. This message comes from the super class and lastly the Snakes eat is printed. So, we use the super key words to access the method of the super class. Okay, now let's use the super keyword to invoke the constructor of the super class. I'll open the Animals super class. The super keyword can also be used to invoke the constructor of the super class. For this, we use the super call statement. First, I will create the empty constructor of this class, public Animals() and I will print the This is animals constructor expression to the console. Now, I will open the Snakes class. Here, I will create the empty constructor of this class. Public Snakes() and I will call the super call statement. This will invoke the constructor of the super class. 

Also, I'll print this message to the console. This is the Snakes constructor. Okay, when the constructor of this class is invoked first the constructor of the super class will be called and then this expression will be printed on the console. So, let's test it. I open the test class. I will delete this line and run it. As you can see, first the constructors of the super class and then the subclass are called. Thus, we learned how to use the super call statement. The general usage of logic of the super keyword is as follows. As a result, while accessing the instance of the super class, that is, methods and variables with the super keyword, we can also initialize its constructor with the super call. Yes. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
2831
Courses
64
Learning Paths
4

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