Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Variables Inheritance and use of Super keyword to access superclass variable

Posted By: Adelchi Fischer     Category: Java     Views: 10393

This article explains about variable inheritance in java with examples.

A class inherits the state and behavior defined by all of its superclasses. State is determined by variables and behaviour is determined by methods. Due to that an object has one copy of every instance variable defined not only by its class but also by every superclass of its class.

Note that a static or instance variable ina subclass may have the same name as a superclass variable. In this case, the variable hides the superclass variable and it can have same type or different types.

It is possible to access hidden variable by super keyword.

Note that super keyword enables you to access the superclass variable and construct a subclass by using the same variable name. You may not use expressions such as "super.super.varName" to access a hidden variable two levels up in the inheritance hierarchy.

 

Syntax to access hidden variable of superclass from subclass

super.varName

varName is a name of the variable in the superclass. This syntax may be used to read or write the hidden variable.

 

Examples of variable Inheritance

Example 1 : Program that illustrates variable inheritance in java.

class Person
{
     String FirstName;
     String LastName;
     String Gender;

class Student extends Person
{
     int id;
     String standard;
     String instructor;
}
 
class InheritanceDemo
{
       public static void main(String args[])
       {
               Student sObj = new Student();
              
               sObj.FirstName = "Rayan";
               sObj.LastName = "Smith";
               sObj.Gender = "Male";
               sObj.id = 10;
               sObj.standard = "1 - A";
               sObj.instructor = "Michel";
 
               System.out.println("First Name : " + sObj.FirstName);
               System.out.println("Last Name : " + sObj.LastName);
               System.out.println("Gender : " + sObj.Gender);
               System.out.println("Id : " + sObj.id); 
               System.out.println("Standard : " + sObj.standard); 
               System.out.println("Instructor : " + sObj.instructor); 
        }
}
 

Output

First Name : Rayan
Last Name : Smith
Gender : Male
Id : 10
Standard : 1 - A
Instructor : Michel
 

Example 2 : Program that illustrates how to access hidden variables of superclass in subclass

class Sample1
{
       int a;
}
 
class Sample2 extends Sample1
{
       String a;
}
 
class VariableInheritanceDemo
{
        public static void main(String args[])
        {
               Sample2 sObj1 = new Sample2();              
               sObj1.a = "This is my test program.";
               System.out.println("Sample 2 : a = " + sObj1.a);
 
               Sample1 sObj2 = new Sample1();
               sObj2.a = 10;
               System.out.println("Sample 1 : a = " + sObj2.a);
         }
}
 
 
Output
 
Sample 2 : a = This is my test program.
Sample 1 : a = 10
  
Share: 



Adelchi Fischer
Adelchi Fischer author of Variables Inheritance and use of Super keyword to access superclass variable is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!