Hi welcome back here in this practical exercise we will be learning method overriding, which is one of the key concepts of object oriented programming. We will create constructors class methods and calling methods of parent class from that of child class. So let's start with this. We have to first import certain libraries that say Java dot Lang, Java dot util, Java dot i, in the beginning that you can use. Then we can create the classes make it public. Public boy, public.
Yes, parents have a constructor. To create a constructor of a class we can. We have to just write the name of the class for which we have to To constructor without having any return type or visibility mode like public, private and protected we have to place a open and open and closed brackets. It is same similar to method what is not method. constructors are called when we create an instance or object of the class. The constructor would display a message on the screen constructor of parent and display message and the display method will display would display a message like create parent method on the screen.
So whenever we create an instance for this class, the constructor will be called. The constructor could be of different types the copy constructor parameterised constructor parameterised constructor could have two different arguments. copy constructor would have address of Go constructor, or under ready. Here we have our child class, which extends the parent or inherits the parent. It will also have a constructor. We have a method as well.
Here we create a constructor for each child, the child open and close braces. You provide a simple System dot out dot print ln message on the screen. Write a constructor child. Again, create a display message here for the child class wide this way, if we have a message or certain functionality that could perform a react System dot out dot print ln Child monitor. After we are done with this, we are going to create a main method our main class where we could actually do things can be used as our super variable. Super is used to call the method of the superclass or parent class.
The Super dot display would call a display method of the parent class. is create a main method is not public static void main string args. You could create object for this class, parent or child class. You're creating object of the child class. With the child object, we could directly access the parent object as well as my parents constructors are accessible from the child. This is the power of inheritance.
Here we have called the display method off the child class. And it will automatically call this method for superclass as well. Just execute this code on a certain error, missing semicolon not a big deal. Call it a semicolon, it will be executed outside here. constructor of parent is called first because it was executed and constructor of child is executed whenever we create a class. Then we have a when creator object.
The child method is called first because it is inside a display message of the child. And when we call super dot display, it will actually display message after pairing our display method and will display the message As we know the constructor is called whenever a class instance is being created when we created the object of the child, it automatically called the parent and the parent class constructor parent was created first, or executed first, on the screen. We never directly call the constructor here. But however the question has been executed whenever we create objects, but we don't create object, the constructor is never executed. So we need to have this. We can always explicitly call the constructor as well by passing the argument of variables to the constructors or they can also return the values as well.