Method over ridding method in java, JAVA Programming

Assignment Help:

Q. Write short on the method over ridding method in java?

Ans. Method over ridding and dynamic binding: We have seen that a method in a super class is inherited by its subclass and is used by the object created by the subclass. Method inheritance enables us to define the method again in the subclass. In the class hierarchy when a instance method the subclass has the same name signature as an instance method (non private) in its super class, then the method in the subclass is said to over ride the method in the super class. When an over ridden method is called from in a subclass, it will always refer to the edition of that method defined by the subclass. The version of the method defined by the super class will be hidden.

Example of method over ridding

Box _ demo java

Class box

{

Int h;

Int w;

Int d;

Box (Int i, Int j, Int k)

{

H = i;

W = j;

D = k;

}

Void show ()

{

System. Out. Println ("box class")

 System. Out. Println (height : " + h)      

System. Out. Println (: "  width+ w)

}

}

Class sub _ box extends box

{

Int i;

Int j;

Int k;

Sub box (Int a, Int b, Int c, Int d, Int e, Int f Int h)

{

Super (d, e, f)

I = a;

J = b;

K = c;

}

Void show ()

{

 

System. Out. Println ("box class")

 System. Out. Println (height : " + h)     

System. Out. Println (: "  width+ w)

}

}

}

Class box _ demo

(

Public static void main (string args[])

{

Sub _ box sb 1 = new sub _ box (10, 20,30, 40, 50, 60);

Sb. Show ();

}

}

Output

Sub _ class of box

Height : 10

Width : 20

Depth : 30

Note when show () method is invoked on an object of type sub _ box the version of show () defined within sub _ box is used, the version of show () method inside sub _ box class over ride declared in box classes.

Application of method over ridding:

Dynamic method binding (dynamic method dispatch or run time binding): method over ridding forms the basic for one java's powerful concept. Dynamic method dispatch is the mechanism by which call to an dynamic method dispatch is an important because this is how java implements run time polymorphism. A super class refers variable can refer to a subclass objects, java uses this fact to resolve calls to over ridden method at run time. When an over ridden method is called through a super class reference, java finds out which version of that method to execute based upon the types of the object being referred to different versions of an over ridden method is called through a super class reference java determines which version of that method to execute based upon the type of the object being referred to at the time of the call referred occurs at this version. Thus this determines is made at run time, when different types of the objects are referred to the different version of an over ridden method of an over ridden method will be called. In other words, it is the kind of the object being referred to (not the type of reference) that determines which version of an over ridden method will be executed. Over ridden method allows java to support run time polymorphism. Polymorphism is essential for OOP for one version. It allows a general class to specify methods that will be common to all of its derivatives while allowing sub classes to define the specific implementation of some of all these methods. By containing inheritance with over ridden methods, a super class can define the general form of method that will be used by all of its sub classes. The ability of existing code libraries to call methods to an instance is a profoundly powerful tool.


Related Discussions:- Method over ridding method in java

Explain what is naming applets in java applets, Explain what is Naming Appl...

Explain what is Naming Applets in java applets? You can give an applet a name by using the NAME attribute of the APPLET element. This allows communication between different app

What is bean factory, A BeanFactory is like a factory class that having a c...

A BeanFactory is like a factory class that having a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whene

How many static initializers can there, How many static initializers can yo...

How many static initializers can you have ? As many as you want, but the static initializers and class variable initializers are implemented in textual order and may not refer

Conditional statements in javascript, Very frequently when you write code, ...

Very frequently when you write code, you desire to perform distinct actions for different decisions. You can employ conditional statements in your code to do this. In JavaScript

Difference between static and non-static variable, What is the difference b...

What is the difference between static and non-static variables? A static variable is associated with the class as a whole rather than with specific instances of a class. Non-st

Arrays, examples of 2D arrays

examples of 2D arrays

Overloaded Methods, QUESTION 3: Overloaded methods Write the overloaded me...

QUESTION 3: Overloaded methods Write the overloaded method named average () for each of the following problems: a) The first method receives THREE (3) integer values and returns

Describe exception classes, Java Programming 1. Discuss any five buzz w...

Java Programming 1. Discuss any five buzz words in Java. 2. Describe exception classes and also explain common exceptions in java. 3. Describe primitive and abstract data

Develop a geonetwork template, Develop a GeoNetwork Template Project Des...

Develop a GeoNetwork Template Project Description: Want a personalized GeoNetwork Template with the consideration of the logo included to this proposal and its colors. The ba

What is aop alliance, AOP Alliance is an open-source project whose goal is ...

AOP Alliance is an open-source project whose goal is to promote adoption of AOP and interoperability between different AOP implementations by describing a common set of interfaces

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd