Java
  Home arrow Java arrow Page 3 - Single Inheritance for Classes 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

Single Inheritance for Classes in Java
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-07-24

    Table of Contents:
  • Single Inheritance for Classes in Java
  • An example of single inheritance: code
  • An example of single inheritance: explanation
  • The role of the default constructor in the super class during single inheritance
  • The role of the default constructor in the sub class during single inheritance
  • The role of constructor overloading during single inheritance

  • 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


    Single Inheritance for Classes in Java - An example of single inheritance: explanation


    (Page 3 of 6 )

    This section explains the code listed in the previous section.  The class “First” is defined with the following members:

    • X
    • Y
    • setValues
    • getSum

    The class “Second” is defined with the following members:

    • getProduct

    The most important part is the following line of definition for the class “Second”:

      public class Second extends First

    The above line defines that the class “Second” gets extended (or inherited) from class “First.”  That means the class “Second” can be considered a child/sub class.  Similarly, the class “First” can be considered a parent/super class. 

    When a class is inherited from the parent, it will generally contain all the members of its parent class (virtually) within itself.  Thus, the class “Second” virtually contains the following members:

    • X
    • Y
    • setValues
    • getSum
    • getProduct

    You can observe that all the members from its parent class (the class “First”) together with its own individual members, are considered to be the members of class “Second.”  All this works behind the screen, magically!

    Now, if we proceed to our “test” frame, we have the following initially:

            Second obj1 = new Second();
            obj1.setValues(10,20);

    I declared an object “obj1” of type “Second.” The second statement shows that we are accessing the method “setValues” from the object “obj1.”  In fact, physically, the method “setValues” does not present in the class “Second” (or in “obj1”). It is made possible because of inheritance.

    We display the sum of two values using the following code fragment:

      int r;
            r = obj1.getSum();
            this.lblMsg.setText("Sum = " + String.valueOf(r));

    Again you can observe that I am calling the method “getSum,” which is available in the parent class. We display the product of two values using the following code fragment:

            r = obj1.getProduct();
            this.lblMsg2.setText("Product = " + String.valueOf(r));

    Now, you can observe that I am calling the method “getProduct,” which belongs to the class “Second” alone (physically).

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys. another article on Java focusing on single inheritance. enjoy
     

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT