Recent

10.Function/Method overriding in Java

Method Overriding in Java/Function overriding in Java

Whenever a parent class and a child class, both are having a same function and the child class function overrides the parent class function then this concept is known as method overriding.

The function overriding concept is used to achieve dynamic binding or run time polymorphism in Java.

An Example of function overriding:

 

 

class Base

{

void show()

{

System.out.println("Show from Base class");

}

}

 

class Child extends Base

{

 

void show()

{

System.out.println("show from Child class");

}

 

public static void main(String[] args)

{

Child c = new Child();

c.show();

}

}

 

/*Output

show from Child class

*/

 

 Access function of Parent class using super keyword:

We can access the parent function after overriding the function by using the super keyword as we did in “Data Hiding”. Here is the example to access parent function.

 

 

class Base

{

void show()

{

System.out.println("Show from Base class");

}

}

 

class Child extends Base

{

 

void show()

{

super.show();

}

 

public static void main(String[] args)

{

Child c = new Child();

c.show();

}

}

 

/*Output

Show from Base class

*/

Question: What was the need of function overriding or why dynamic binding at runtime required?

Answer: To decide which function to execute at runtime.

·         Now, let us understand this with the help of an example:

Without overriding the function:

 

 

class Base

{

void info()

{

System.out.println("This car is wonderful");

}

}

 

class Child extends Base

{

public static void main(String[] args)

{

Child c = new Child();

c.info();

}

}

 

/*Output

This car is wonderful

*/

In this program, the output will be same each time if we do not use function overriding or we will have to define some more functions for same work. So what if we have to give some details about the car and car is different each time.

This is how we managed this by using function overriding:

 

 

class Base

{

void info()

{

System.out.println("This car is wonderful");

}

}

 

class Child extends Base

{

 

void info() //function overriding

{

System.out.println("Jaguar is a wonderful car");

}

 

public static void main(String[] args)

{

Child c = new Child();

c.info();

}

}

 

/*Output

Jaguar is a wonderful car

*/

We need to satisfy some conditions for function overriding to work in Java. We have to satisfy 3 conditions:

1) Parent class function and a child class function both must be having a return type as a referenced that means both functions must return the objects of some classes. 
***** Data type is referenced if we return the value of objects of a class.
2) The classes whose objects are returned from the parent class function must be having the parent to child relationship.
3) Parent class function should return parent class object and child class function should return the child class objects

Some Questions:

Question: Can we override static methods?

Answer: No, because static method is bound with class whereas instance method is bound with object. Static methods belongs to class area and instance belongs to heap area.

Question: Can we override main() method?

Answer: No, because main() method is defined as static and static methods cannot be overridden.

Question: What is the difference between function overloading and function overriding?

Answer: Some basic differences between function overloading and function overriding:

1.    In case of function overloading, we have several functions with the same name, but they have different argument. But in case of function overriding, we have function with same name and arguments.

2.    In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.

3.    In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a parent class and child class methods.

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India