Home arrow Java arrow Page 3 - Overriding Methods and Inheritance in Java
JAVA

Overriding Methods and Inheritance in Java


In this article, I shall discuss the following topics on programming with OOPS (especially single/multilevel inheritance) in Java using NetBeans IDE: overriding in single inheritance, overriding in multilevel inheritance, and how to call super class methods during overriding.

Author Info:
By: Jagadish Chaterjee
Rating: 3 stars3 stars3 stars3 stars3 stars / 19
August 07, 2006
TABLE OF CONTENTS:
  1. · Overriding Methods and Inheritance in Java
  2. · An example of overriding methods in single inheritance: code
  3. · An example of overriding methods in single inheritance: explanation
  4. · An example of overriding methods in multilevel tree type inheritance: code and explanation
  5. · An example of overriding methods in multilevel chain type inheritance: code and explanation
  6. · How to call super class methods during overriding

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Overriding Methods and Inheritance in Java - An example of overriding methods in single inheritance: explanation
(Page 3 of 6 )

This section will explain the code listed in the previous section. The class "Person" is defined with the following members:

  • name
  • setName
  • getDetails

The class "Lecturer" is defined with the following members:

  • qualification
  • getDetails

As the class "Lecturer" is inherited from "Person," it virtually contains the following members:

  • name
  • setName
  • getDetails
  • qualification

You must note that "getDetails" is available in both super and sub classes. From the class "Lecturer" point of view, it considers its own local method "getDetails," rather than the class "Person."  Now, if we proceed to our "test" frame, we have the following initially:

        Person p1 = new Person("Jag");
        Lecturer l1 = new Lecturer("Chat", "M.Sc.");

The above will create two objects, "p1" (of class "Person") and "l1" (of class "Lecturer").  We call the method "getDetails" from both of those objects and display the information on the form using the following lines:

        this.lblMsg.setText(p1.getDetails());
        this.lblMsg2.setText(l1.getDetails());

In the case of "p1.getDetails," it executes the method "getDetails," which belongs to the class "Person." In the later case of "l1.getDetails," it executes the method "getDetails," which belongs to the class "Lecturer."  Make sure that it doesn't execute the method "getDetails" which is available in the "Person" class from the "Lecturer" object.

That means the method "getDetails" in the "Lecturer" class is given more preference than "getDetails" in the "Person" class for any "Lecturer" object.  This is what is called overriding the methods.


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials