Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use a variable from a superclass to a subclass?

I have a superclass and I want to use a variable that is inside this superclass into my subclass. How can this be possible?

like image 237
Mikel Avatar asked Jun 07 '13 20:06

Mikel


People also ask

How do you change a super class variable in a subclass?

You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.

Can a subclass access superclass variables?

Rule:A subclass inherits all of the member variables within its superclass that are accessible to that subclass (unless the member variable is hidden by the subclass). inherit those member variables declared with no access specifier as long as the subclass is in the same package as the superclass.

How a superclass variable can reference a subclass object?

First approach (Referencing using Superclass reference): A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. If the methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.

Can a subclass use a private variable from a superclass?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.


2 Answers

Just make the field protected, meaning that it should be visible to all derived classes.

like image 189
SLaks Avatar answered Nov 15 '22 16:11

SLaks


I have a superclass and I want to use a variable that is inside this superclass into my subclass. How can this be possible?

If your variable is declared as protected or public (or) your variable hasdefault access privileges(in which case you don't specify with any keyword) and they are in same package(--> You can access it in the subclass directly. You may use this keyword if you are specific.

Example:

   public class A{
         protected int field=1;
   } 
   public class B extends A{
         public B(){
             System.out.println(this.field);
         }
         public static void main(String args[]){
           new B();
        }
   }

Please note that variable-overriding is not possible. If you have a variable with the same name as in super class then you are out of luck to directly access it. Then you may use super keyword.

       public class A{
             protected int field=1;
       } 
       public class B extends A{
             protected int field=3;
             public B(){
                 System.out.println(this.field);
                 System.out.println(super.field);
             }
        public static void main(String args[]){
           new B();
        }
       }
like image 28
pinkpanther Avatar answered Nov 15 '22 16:11

pinkpanther