faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 13 people (77%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Java: Class: Inheritance: Create: Simple: Can you give an example of inheritance?

Sep 30th, 2003 05:58
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2003 - 07:42 pm ------------------

Java: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?

1. In general:

Steps: Overview:

 1. -Create a class <yourclassnameparent>:

http://www.faqts.com/knowledge_base/view.phtml/aid/24762/fid/163

 2. -Create another class <yourclassnamechild>:

     1. To indicate that you want to use inheritance, use the following
        notation:

      class <yourclassnamechild> extends <yourclassnameparent> {
       // do something
       // e.g. -add new variables, functions and procedures
       //
       //      and or
       //
       //      -overwrite existing variables, functions
       //       and procedures of that 'parent' class
      };

     2. So you indicate that your class is inheriting:

        1. first the word 'class'

        2. followed by the name of 'child' class

        3. followed by the word 'extends'

        4. followed by the name of the 'parent' class

---
---

2. In specific:

 1. -Create a parent class 'addressC',
     and give the code for the procedures or functions used in
     that class:

      class addressC {
       //
       protected String nameS;
       protected String streetS;
       protected String cityS;
       protected String countryS;
       protected String telephoneS;
       protected String emailS;
       //
       // a 'getter' for the 'parent' class
       public void PROCView() {
        System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' 
+ '-' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
       };
       // the 'constructor' for the 'parent' class
       public addressC( String nameS, String streetS, String cityS, 
String countryS, String telephoneS, String emailS ) {
        this.nameS = nameS;
        this.streetS = streetS;
        this.cityS = cityS;
        this.countryS = countryS;
        this.telephoneS = telephoneS;
        this.emailS = emailS;
       };
       // the 'destructor' for the 'parent' class
       protected void finalize() {
       };
      };

http://www.faqts.com/knowledge_base/view.phtml/aid/16104

 2. -Create another child class 'addresscompanyC',
     and give the code for the procedures or functions used in
     that class:

      class addresscompanyC extends addressC {
       //
       private String companynameS;
       //
       // a 'getter' for the 'child' class
       public void PROCView() {
        System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' 
+ '-' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + 
companynameS );
       };
       // the 'constructor' for the 'child' class (which calls 
the 'constructor' of the 'parent' class first, this must be the first 
line)
       public addresscompanyC( String nameS, String streetS, String 
cityS, String countryS, String telephoneS, String emailS, String 
companynameS ) {
        super( nameS, streetS, cityS, countryS, telephoneS, emailS );
        this.companynameS = companynameS;
       };
       //
       // the 'destructor' for the 'child' class
       protected void finalize() {
        super.finalize();
       };
      };

 4. -Declare the variables which you intend to use with your class:

      addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
      addressC vanessaO = new addressC( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );

      addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
      addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );

 5. -Supply some source code where you use your class
     (create another class, which contains the 'main()'
      method. Then save this file with the same name
      as that class (e.g. 'testinheritanceC.java')

      class testinheritanceC {
       public static void main( String args[] ) {
        //
        // initialize variables representing that 'parent' class:
        //
        addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
        addressC vanessaO = new addressC
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
        //
        // initialize variables representing that 'child' class:
        //
        addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
        addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
        //
        // do something
        //
        //
        // view variables stored in the variables representing 
that 'parent' class:
        //
        knudO.PROCView();
        vanessaO.PROCView();
        //
        // view variables stored in the variables representing 
that 'child' class:
        //
        knudcompanyO.PROCView();
        vanessacompanyO.PROCView();
        //
        // finally release the memory used by that variable, after 
use, when you do not need it anymore
       }
      }

 6. -This gives alltogether the following source code:

      // save as 'testinheritanceC.java'

      // --- 1. declaration of the 'parent' class --- //

      class addressC {
       //
       // it must be 'protected', because
       // the 'child' class must be able to read their values
       //
       protected String nameS;
       protected String streetS;
       protected String cityS;
       protected String countryS;
       protected String telephoneS;
       protected String emailS;
       //
       // --- 1.1 functions and procedures of the 'parent' class --- //
       //
       // 1.1.1 a 'getter' for the 'parent' class
       public void PROCView() {
        System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' 
+ '-' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
       };
       //
       // 1.1.2 the 'constructor' for the 'parent' class
       public addressC( String nameS, String streetS, String cityS, 
String countryS, String telephoneS, String emailS ) {
        this.nameS = nameS;
        this.streetS = streetS;
        this.cityS = cityS;
        this.countryS = countryS;
        this.telephoneS = telephoneS;
        this.emailS = emailS;
       };
       //
       // 1.1.3 the 'destructor' for the 'parent' class
       protected void finalize() {
       };
      };

      // --- 2. declaration of the 'child' class --- //

      class addresscompanyC extends addressC {
       //
       private String companynameS;
       //
       // --- 2.1 functions and procedures of the 'child' class --- //
       //
       // 2.1.1 a 'getter' for the 'child' class
       public void PROCView() {
        System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' 
+ '-' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + 
companynameS );
       };
       //
       // 2.1.2 the 'constructor' for the 'child' class (which calls 
the 'constructor' of the 'parent' class first, this must be the first 
line)
       public addresscompanyC( String nameS, String streetS, String 
cityS, String countryS, String telephoneS, String emailS, String 
companynameS ) {
        super( nameS, streetS, cityS, countryS, telephoneS, emailS );
        this.companynameS = companynameS;
       };
       //
       // 2.1.3 the 'destructor' for the 'child' class
       protected void finalize() {
        super.finalize();
       };
      };

      // --- 3. do something --- //

      class testinheritanceC {
       public static void main( String args[] ) {
        //
        // 3.1 create and initialize variables representing 
that 'parent' class:
        //
        addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
        addressC vanessaO = new addressC
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
        //
        // 3.2 create initialize variables representing that 'child' 
class:
        //
        addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
        addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
        //
        // 3.3 do something
        //
        //  3.3.1 view variables stored in the variables representing 
that 'parent' class:
        //
        knudO.PROCView();
        vanessaO.PROCView();
        //
        //  3.3.2 view variables stored in the variables representing 
that 'child' class:
        //
        knudcompanyO.PROCView();
        vanessacompanyO.PROCView();
        //
        // finally the memory used by that variable will be released 
(by Java), after use, when you do not need it anymore
       }
      }

 7. -When you run this source code, you will see e.g. a text screen
     with the following information:

> javac.exe c:\temp\testinheritanceC.java

> java testinheritanceC

myname - mystreet 1 - mycity - mycountry - 01-2345678 - myemail@address

anothername - anotherstreet 1 - anothercity - anothercountry -
01-3456789 - anotheremail@address

myname - mystreet 1 - mycity - mycountry - 01-2345678 - myemail@address
- thecompany1name

anothername - anotherstreet 1 - anothercity - anothercountry -
01-3456789 - anotheremail@address - thecompany2name

Press any key to continue . . .

---
---

Note: this source code has been tested and worked OK
[kn, ni, mo, 29-09-2003 18:28:33]

---
---

Internet: see also:

C#: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24889/fid/791

C++: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24882/fid/163

Delphi: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24880/fid/175

Java: Class: Inheritance: What is inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24864

----------------------------------------------------------------------