Search

Saturday, May 8, 2021

Method overriding in Java | Java method overriding

 

Method overriding in Java



Java method overriding. If you want to get knowledge and clear understanding about method overriding in Java. Then its a best place for you, here i will teach you in detail about Java method overriding with very easy to understand examples. 


Java Method Overriding

In Java, we have concept of inheritance which means we can have a class which is derived from another class known as Parent class which provides features to its child class. Now Java method overriding comes when we have a method in child class which is already available or present inside the parent class.

We usually do method overriding because with the help of this, child class can define a method already present in the parent class according to its own requirements without actually effecting the original code present in the parent class method.

If we perform method overriding, then the method present inside the parent class is known as overridden method in Java and it is known as overriding method with respect to child class.


For Example

Suppose we have two classes, one is A and one is B. B class is inheriting class A. In class A, we have a method void ftn() and in class B, we also have the same method i.e. void ftn(), but it is defined in the parent class according to the parent class and child class wants to define it according to its own requirements. So this concept is known as method overriding.

Class A

{

        Public void ftn()

        {

        System.out.println(“parent class method”);

        }

}

Class B extends A

{

Pubic void ftn()

        {

        System.out.println(“child class method”); //overriding the parent class method

        }

        Public static void main (String ar[ ])

        {

        B obj = new B();

        Obj.ftn();

        }

}


Output will be:

Child class method

 

The major advantage of the method overriding is that child class can give its own implementation to the method already defined in the parent class and also if a parent class has lots of child classes and most of them want to use the method of parent class as it is, they can do it and if some class wants to change the method implementation, it can also do it.


Important points about Java method overriding

1.   Parameter of the method present inside child class must match with the parent class method

2.   If parent class method has public access modifier, then child class method cannot contain private or protected modifier

3.   Java static methods or final methods or private methods cannot be override because they belong to the class

4.   We have dynamic binding concept in case of method overriding which means binding of the methods will be done at runtime.

 

Use of super keyword in Java Method Overriding

With the help of Java super keyword, we can call the parent class constructor or parent class method in method overriding. For example, if we have a method inside parent class pubic void ftn() and we also have this method inside child class, to call the parent class method, we will use super.ftn(). Same is the case with constructor calling.


Example Program

Class A

{

Public void ftn()

    {

    System.out.println(“parent class method”);

    }

}

Class B extends A

{

 

Public void ftn()

    {

    Super.ftn(); //call to parent class ftn method

    System.out.println(“child class method”);

    }

    Public static void main (String ar[ ])

    {

    B obj = new B();

    Obj.ftn();

    }

}


Output will be

Parent class method

Child class method


Java Final keyword to prevent method overriding

In Java, it is possible to prevent method overriding. As we know that Java methods can be override by child class, but if super class or parent class wish to not override its methods, it is possible and we have to use final keyword with the methods to stop overriding them by child classes. 


If a method in super class is defined with final keyword and any attempt by the child class to override it will give you error. For example, we have two classes, class A and class B. Class A is the parent class and class B is the child class. In class A, we have a method ftn() and it is define with the final keyword like final void ftn(), and in the child class B, if someone is trying to override this method, compiler will generate an error.


Example Program

class A

{

final void ftn()

{

System.out.println("cannot be override");

}

}

class B extends A

{

void ftn()

{

System.out.println("Child class is overriding final method");

}

}

public class MethodOverridingTest

{

    public static void main (String ar[ ])

    {

    B obj = new B();

    obj.ftn();

    }

}


Output will be:

error


In the above program, we are getting error because child class B is trying to override Parent class ftn method, which is define with final keyword to prevent method overriding. So we cannot override it.


Java final keyword to prevent Inheritance

Some time, in Java language, we do not want that a class inherits another class because of some security reasons. Now if we want to achieve this, we can prevent inheritance using final keyword in Java. If a class is defined with final keyword, and if some another class is trying to make it a parent class, compiler will generate an error because final class cannot be inherited. 


We can explain it with the help of an example. Suppose we have a class A and it is define using Java final keyword. We have another class i.e. class B and this class is extending the class A which is final class. We will get an error by the compiler that this class cannot be inherited.


Example Program

final class A

{

//some coding here

}

class B extends A //inheriting final class

{

//some coding here

}

public class PreventInheritance

{

    public static void main (String ar [] )

    {

    B obj = new B( );

    }

}


Output will be:

error. Cannot inherit from final class


Video Lecture:










No comments:

Post a Comment