 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you method overriding in Java. Before starting, you can note my information. You can connect with me by following these details which are available in front of you. You can subscribe my YouTube channel by clicking on the bottom right corner. If you want to download my app, you can use the name tagms which is available on Google Play. First, I am going to explain you the concept of method overriding and then I will be demonstrating an example. The first point says in a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. So with the help of this first statement, we can understand that both super and subclasses have a function or method with same name and or with same type signature. And this will be happen if we are implementing inheritance. Second point says a superclass and subclass both have methods with same name and same argument list. And their return type must also be same otherwise it would be a compile time error. It is simple. Third point says instance methods can override instance methods and static methods can override static methods. This you have to remember. Fourth point says when we override a method, the overridden method in the subclass can be less restrictive. But it cannot be more restrictive. It would be a compile time error if we try to do so. This is because in case of overriding, we call methods of subclass. That's why it should be less restrictive. Next point says methods are inherited but constructor of class are never inherited. So we cannot inherit constructors, we can only inherit methods. Next point says a superclass reference variable can refer to the object of the subclass but reverse is not allowed. It means superclass reference can refer to the subclass object but subclass reference cannot refer to the superclass object. Last point says we cannot override field or instance variable of the class. This is simple. Now I am going to demonstrate an example so that you can understand the concept of overriding or you can say method overriding. You can see first class whose name is shape. Inside shape class, I have implemented first method name which is printing shape. Second method is area which is printing shape area with the help of system.output println. This is implementation of shape class. Then I am implementing shape class into square class. This is inheritance. Then it has square class contains a variable length. It is private. Its value is 5. Then it also contains name method which is displaying square with the help of system.output println. And it is implementing area which is calculating area by multiplying length into length. You can see that I have implemented name and area in shape class as well as these two methods are implemented in square class. And square class is inheriting shape class. So name and area which are implemented in square class will be considered as overridden methods. Now third class is rectangle which is also inheriting shape class. It contains two attributes length and width. Then again it is overriding name method. It is displaying rectangle. Then area method. It is calculating area by multiplying length and width. So it is displaying area of rectangle. Here name and area are overridden because here shape class is inherited and shape class also contains name and area methods. So friends here superclasses shape it contains two subclasses. First one is square and second one is rectangle. Now at last I have implemented test class. Inside test class I have created a reference of shape class. Then object of shape class is created. It is calling name and area. Here name and area of shape class will be called. Then reference of superclass that is shape and references as is accessing object of square class. So as I have already discussed that reference of superclass can refer the sub object. So here as is superclass reference and square is creating object of subclass. So it is valid statement. Now as dot name and as dot area will call name and area of square class because of method overriding. Here references of superclass but methods that we are calling belongs to subclass. That will be overridden and methods of subclass will be executed here. Similarly in last three statements as which belongs to shape class is referring to rectangle class object. This is also correct and here name and area of rectangle class will be called. They will override methods of shape class here. Now I am going to execute this program so that you can see the output. You can see shape and shape area is printed. Shape is name and shape area because of area method which is defined in shape class. Then square and area is 25. These two statements is displayed with the help of square methods and last two statements are displayed with the help of name and area methods which are defined in rectangle class. So friends you might have understood that if we implement methods with same name in super in subclass and if we create reference of superclass and object of subclass. Then object of subclass overrides methods of superclass and the output is displayed as per the methods defined in subclass. So this is simple demonstration for method overriding. You can implement this code in your system so that you can understand it well. If you want to watch more programming related videos you can subscribe my YouTube channel. You can connect with me by following these details which are available in front of you. Thank you for watching this video.