Home arrow Java arrow Page 2 - 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: code
(Page 2 of 6 )

I already introduced different types of inheritance in my previous articles. In this section, I shall introduce you to the concept of overriding methods in inheritance.  The concept of overloading is totally different from that of overriding methods. Let us start with an example.

Open your NetBeans project (as given in my previous article), and modify your code in "Person.java" so that it looks something like the following:

  public class Person {

    String name;
    /** Creates a new instance of Person */
    public Person() {
    }

    public Person(String n) {
        name = n;
    }
    public void setName(String s) {
        name = s;
    }
   
    public String getDetails() {
        return "Name = " + name;
    }
  }

Modify your code in "Lecturer.java" so that it looks something like the following:

  public class Lecturer extends Person {
    String qualification;
    /** Creates a new instance of Lecturer */
    public Lecturer() {
    }

    public Lecturer(String n, String q) {
        name = n; 
        qualification = q;
    }

    public String getDetails() {
        return "Name = " + name + ", Qualification = " + qualification;
    }
  }

Now go back to the frame "test.java."  Double click on it to open and then double click on the button to open source view. Within the source view, modify your "buttonActionPerformed" in such a way that it looks like the following:

  private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
  // TODO add your handling code here:
        Person p1 = new Person("Jag");
        Lecturer l1 = new Lecturer("Chat", "M.Sc.");
        this.lblMsg.setText(p1.getDetails());
        this.lblMsg2.setText(l1.getDetails());
    }
         

The next section will explain the above code.


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials