public class A {
	String username = "Unknown";

	public A() {
		username = "Virux";
		B.method();
	}
}

public class B extends A {
	public void method() {
		System.out.println(super.username);
	}
}

Obviously my actual code is much more complex than this.
But how can I get B to see A's 'username' without passing any parameters? Ie; I can't pass 'username' through like "B.method(username);"

I also need this to be an instance, or whatever you want to call it. I need to be able to have multiple A or B objects in an array of Threads.
So if you just try to change 'username' to public static, and my code starts a new thread(that starts a new A or B object) then the 'username' gets changed for all of the other threads. I can't have this.
I was hoping I can do this without an array of 'username' variables and as you can see I'm not too advanced in inheritance yet.

So, all in all: I have a class with an array of threads. "Thread[] threads = new Thread[5];" or whatever. And then I'll have each thread start a new A or B object. In thread: "public void run() { new A(); }" or it can be "new B();" it doesn't matter. Once the A/B object has been started, the 'username' variable will be set in the A object. And then the A object will call a method in B, and B will use the 'username' variable for what I need it for.

Let me know if you need me to clairify anything. Thanks.

Recommended Answers

All 4 Replies

Class B has the username variable because it inherits A.
But If you want to access it then you need to choose how to access it: private, public protected.

I think that since you didn't add anything it is private so naturally you cannot access from outside that class.

You can make it public, but then anyone would be able to access it.
You could make it protected, or better private and also add public method that returns the username.

public class A {
	private String username = "Unknown";

	public A() {
		username = "Virux";
	}

        public String getUsername() {
            return username;
        }
}

public class B extends A {
       public B() {
         super();
       }

	public void method() {
		System.out.println(super.getUsername());
	}
}

Also the code you posted will not compile, so I removed the line that would give you the error. The B.method() cannot execute because the method() is not static.


EDIT:
After reading the rest of your post, if you create an A instance the you cannot call the "method" because it is part of the B class.
If you create a B class and call "method" then it will print whatever the "username" variable returns.

Perfect, perfect.
Thank you.

Now how do I call B.method() then?
If I do it as "B.method()" everything would have to be static.

And I think static variables can be changed by other objects. ie; another object instance in a different Thread

If you make method static then everything needs to be static and you don't want that as you said.
So create an instance of B and call it like I told you:

B b = new B();
b.method();

Now, are you saying that you want to create an 'A' instance and somehow call the "method"?
Or are you just trying to access username:

A a = new A();
System.out.println(a.getUsername());

B b = new B();
b.method();

Or:

public class A {
      private String username = "Unknown";
       
      public A() {
      username = "Virux";
      }

      public String getUsername() {
        return username;
      }


      public void method() {
          System.out.println(username);
      }
}


      public class B extends A {
        public B() {
           super();
        }
      }

And call the method like this:

A a = new A();
a.method();

B b = new B();
b.method();

A [] arrayA = new A[2];
arrayA[0] = new A();
arrayA[1] = new B();
for (int i=0;i<arrayA.length;i++) {
    arrayA[0].method();   
    arrayA[1].method(); // this is the B instance. If you override the method in the B class then it would be not the A method called but B.
}

// And if you put additional stuff in the B class, in order to access them then:
if (arrayA[1] instanceof B) {
  B b = (B)arrayA[1];
  b.someOtherMethod(); // defined only in B
}

Try doing this, and then run the example above:

public class A {
      private String username = "Unknown";
       
      public A() {
      username = "Virux";
      }

      public String getUsername() {
        return username;
      }


      public void method() {
          System.out.println("A:"+username);
      }
}


      public class B extends A {
        public B() {
           super();
        }
    
        public void method() {
          System.out.println("B:"+super.getUsername());
        }
      }
A a1 = new A();
a1.method();

A a2 = new B();
a2.method();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.