Super vs Super()
Start course
Difficulty
Beginner
Duration
1h 46m
Students
10
Ratings
5/5
starstarstarstarstar
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.

Learning Objectives

  • Inheritance in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello, my friends. In this lesson, we will talk about the super keyword and the super call expression. We talked about the 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 the 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 references 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 in three different types of ways: To 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.

All right, without further ado, let's look at the use of the keyword super and the super call expression. First, I will create a super class named Animal. Right-click on the 'Source' folder in the Object Oriented programming project and select the New and Class options. The package name can be superexample. The class name can be Animals, and click the 'Finish' button. 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 the super class Animals: boolean isMammal = true; Okay, now I will create the subclass. Right-click on the 'Superexample' 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 extends 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 snake. 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 classes both have a common property isMammal. If we print the 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 'Superexample' class and select the New and Class options. The class name can be SuperTest.

I will check the check box and click the 'Finish' button. I'll 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 use the super keyword to access the instance of the super class. Got it? Now, let's use the super keyword to access the methods of the super class. I will open the animal 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 animal super class: public void eat() and this method will print the ("Animals eat") on the console. Now, let's open the subclass snake. In this class, I will 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 the subclass: public void bite().  And the third method will be the printing of the 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. 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 open the SuperTest 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 keyword to access the method of the super class. Got it?

Now, let's use the super keyword to invoke the constructor of the super class. I will open the Animal 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 System.out.println  ("This is the 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 will print this message to the console, ("This is the Snakes constructor"). 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. Good job. The general usage logic of the super keyword is as follows. As a result, while accessing the instances of the super class, that is methods and variables, with the super keyword, we can also initialize its constructor with a super call. So, let's take a short break here my friends, and I'll see you in the next lesson.

 

About the Author
Students
1870
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.