Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Method Overriding 
 

Overriding is another useful feature of object-oriented programming technique.

 

Method Overriding

                         

Overriding is another useful feature of object-oriented programming technique. It provide the facility to redefine the inherit method of the super class by a subclass. The method overriding is used to invoking the parent class method by a child class. It is possible when a child class extends the parent class then all the attributes and the methods of parent class inherit by child class. The methods name and the parameters pass in the subclass are same as superclass methods. The restriction of the access modifier of the subclass method should not more than the superclass method e.g. if the superclass method is protected, the overriding method may be protected or public.

Here is an example of the method overriding. Create a class "Override" and create two subclasses "One" and "Two" and define a same method in both of them. When the main class call the method the method executes on behalf of their definition. In the given example we are trying to illustrate the same technique.

Here is the Code of the Example :

"Override.java"

import java.lang.*;

class One {
  int first = 100;
  int func() { return first; }
  }
  class Two extends One {
    int first = 200;
    int func() { return -first; }
    }
    public class Override {
      public static void main(String args[]) {
        Two obj2 = new Two();
        System.out.println(obj2.first);
        System.out.println(obj2.func());
        One obj1 = (One) obj2;
        System.out.println(obj1.first);
        System.out.println(obj1.func());
      }
    }

Here is the Output of the Example :

C:\roseindia>javac Override.java

C:\roseindia>java Override
200
-200
100
-200

Download This Example :

                         

» View all related tutorials
Related Tags: c class io nested definition source while ini member e il it not init art ce in no part as

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

The test says:
The restriction of the access modifier of the subclass method should not more than the superclass method e.g. if the superclass method is protected, the overriding method may be protected or public.

But isnt the access for "pulic" higher than "protected"- The explanation says: "the subclass method should NOT more than the superclass method".
The statement sounds a little contradictory.

Can some explain?

Posted by Vidhya on Tuesday, 09.16.08 @ 09:19am | #79779

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.