Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

setting variable values of inherited classes

Posted on 2007-10-18
7
Medium Priority
?
166 Views
Last Modified: 2010-03-30
suppose i have three classes
base
subclass
driver
inside base clase i have two variable say int a and int b
subclass extends base
and inside the driver i have
subclass c=new subclass();
How would i write such that when i instantiate the subclass the varaibles a and b both get set to 1.
I dont' want it such that i have  in the constructor of subclass becuase if i extend base to another subclass and instatiate it, i want a and b to be set to 1 again.
So basically i want it such that when the subclass constructor is called, the base's constructor also get called ( since i could have a=b=1 inside that) or somthing to that effect
0
Comment
Question by:angelblade27
  • 4
  • 2
7 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 20106849
If a and b must ALWAYS be initialized to 1, why don't you do that in base{}?
It can be done in a constructor or in a static block:

static{ a = b = 1; }

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20106853
Or simply do it outside constructors:

public class base
{
    int a = 1;
    int b = 1;
...........................
}


;JOOP!
0
 
LVL 1

Accepted Solution

by:
mnadeem22 earned 500 total points
ID: 20107004
I have created the sample program to solve your problem, check it out, save the class in seperate files (i beleive you know it) compile and then run the Driver class.

public class Base
{
      private int a, b;

      public Base()
      {
      }

      public Base(int a, int b)
      {
            this.a = a;
            this.b = b;
      }

      public void printValues()
      {
            System.out.println("Value of 'a' is: " + a + " and value of 'b' is: " + b);
      }
}



public class SubClass extends Base
{
      public SubClass()
      {
            super(1,1);
      }

      public void printValues()
      {
            super.printValues();
      }
}



public class Driver
{
      public static void main(String arg[])
      {
            SubClass c = new SubClass();
            c.printValues();
      }
}

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 24

Expert Comment

by:sciuriware
ID: 20107167
mnadeem22, you overlooked:

>>> I dont' want it such that i have  in the constructor of subclass becuase if i extend base to another subclass

;JOOP!
0
 

Author Comment

by:angelblade27
ID: 20118585
well yes i could set it inside the base class itself but the  the example i gave (setting a=1 dynamically) was more to get the logic actually. In reality i will have a parameter called url and i will pass it through command line arguments( when i run the driver class). So i would be passing the args variable into my constructor (when i instatiate the sub class) and my base class would parse the values and set the url variable accordingly.
0
 
LVL 1

Expert Comment

by:mnadeem22
ID: 20122349
I have another suggestion then, you can make the variables 'a' and 'b' protected in the base class so that you can initiate them in the subclass's constructor.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20122957
mnadeem22, this time you forgot the word ONLY in your statement.

Now, give us a reason why the questioner would need this.

;JOOP!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question