Java
  Home arrow Java arrow Page 4 - Overriding Methods and Inheritance in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Overriding Methods and Inheritance in Java
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 18
    2006-08-07

    Table of Contents:
  • Overriding Methods and Inheritance in Java
  • An example of overriding methods in single inheritance: code
  • An example of overriding methods in single inheritance: explanation
  • An example of overriding methods in multilevel tree type inheritance: code and explanation
  • An example of overriding methods in multilevel chain type inheritance: code and explanation
  • How to call super class methods during overriding

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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."

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys! another contribution on working with OOPS in Java using NetBeans IDE....
     

    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...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek