Home arrow Java arrow Page 4 - 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 multilevel tree type inheritance: code and explanation
(Page 4 of 6 )

In the previous sections, I simply gave an example of overriding methods in a single inheritance.  Now, I shall discuss overriding methods in a multilevel inheritance (tree type).

Add a new class, "Student," to the existing solution. Modify the code in "Student.java" in such a way that it looks like the following:

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

    public Student(String n, String c) {
        name = n;
        course = c;
    }

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

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:
        Student s1 = new Student("Jag","B.Sc.");
        Lecturer l1 = new Lecturer("Chat", "M.Sc.");
        this.lblMsg.setText(s1.getDetails());
        this.lblMsg2.setText(l1.getDetails());
    }
                

According to the above code, it now executes the method "getDetails" of "s1" and another method, "getDetails" of "l1".  But none of it really executes "getDetails" of the class "Person."


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