954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can I access methods or attributes from a subclass

Hey all. Iam new to java and I have been trying to do something that has played me abit. I want to use an attribute or a method from a subclass and accesing it from the super class. Something like this

class A{
//some code
}

class B extends A{
public String a;

public String getMeth()
{
//
return something;
}

}


The above is just a sketch for illustration purposes. But what I want to do is to use the method or the attribute in the "class b" in A. Thanx for any help

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

You cannot use what you declare in B inside A.
Only the other way around since it is B that extends A, so B can access what you declare in A

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

make the methods and fields public in the master class?

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 
make the methods and fields public in the master class?


making the superclass containing all the possible variables and methods of the classes that inherit it?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
You cannot use what you declare in B inside A. Only the other way around since it is B that extends A, so B can access what you declare in A


I know the other way round can be done. What about accesing the methods? I dont want to use overridding i dont know if there is a way using casting or polymorphism because that stuff is alittle confusing to me at the moment. Thanx

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 
public class A {
   public void someMethod_A1() {

   }

   public void someMethod_A2() {

   }
}
public class B extends A {
   public void newMethod_B1() {
        //BOTH are the same:

         someMethod_A1(); //calls the method from super class
         super.someMethod_A1(); //exactly the same as the above
   }


// BUT if you also declare this:
    public void someMethod_A2() {

   }

//AND

    public void newMethod_B2() {
         this.someMethod_A2(); //calls the method from the class B (this class)

         super.someMethod_A2(); //calls the method from the class A (super class)
   }
}


I didn't quite understand what you are asking in your final post so I posted this example.
Let us know if you have any comments on it.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

I get that kind of calling. What i want to do is the other way round. I want to access the method in the subclass from the superclass. Is there any way this is possible?

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 
I get that kind of calling. What i want to do is the other way round. I want to access the method in the subclass from the superclass. Is there any way this is possible?


if I understand this correctly, you have (for instance) a class A (superclass) and a class B (subclass) which extends A, and you want B to be able to use a method in A?

just call it, you inherit the data and methods from A, so they're accessible for B

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

if I understand this correctly, you have (for instance) a class A (superclass) and a class B (subclass) which extends A, and you want B to be able to use a method in A?

just call it, you inherit the data and methods from A, so they're accessible for B

That is not it. I want A to be able to use a method in B..Is that possible

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

You mean this? :

public class A {
   public void method_A1 {
      
        //calling method from B
        newMethod_B1(); 
   }
}
public class B extends A {
   public void newMethod_B1() {
       
   }
}


No it cannot be done. What I wrote above is wrong. Although however you can do this, but has nothing to do with inheritance:

public class A {
   public void method_A1 {
      
        B b1 = new B();
        b1.newMethod_B1(); 
   }
}


You can create and call any class from anywhere, but like I said it has nothing to do with one class extending the other

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

you propably could code a way to do that, but what would be the use of that?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Yep. That is what I was looking for. Thanks alot

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 
you propably could code a way to do that, but what would be the use of that?


well like I said am just tryna learn and satisfy my ego.

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 
That is not it. I want A to be able to use a method in B..Is that possible

I think Object creation might shed some light on this issue...

If you create an Object of type A, A is at least of its class and additionally any other class it inherits from.

A cannot, at runtime, extend its static capabilities. Inheritance is resolved at compile time, so when A is defined and its super classes are known, objects of type A will not be able to do magical things like call methods (within the scope of A) that exist in a subclass that A isn't even aware of.

B on the other hand is at least of type B and additionally any other class it inherits from. B inherits from A so B can call methods that exist within the A class it is derived from.

If you want dynamic behavior where A can call B-like methods, you shouldn't rely on inheritance to do the trick and instead use a Pattern to solve the problem--

public class BaseClass{

	public void doSomething(){
		System.out.println("Do Something! O_O");
	}

}
public class DerivedClass extends BaseClass{

	public void moreWorkToDo(){
		System.out.println("Whoo! More work >_<");
	}

}
public class ModularClass extends BaseClass{

	public BaseClass myDynamicA = new BaseClass();

	@Override public void doSomething(){
		myDynamicA.doSomething();
	}

	public void setRef(BaseClass theRef){
		myDynamicA = theRef;
	}

	// I know this is sloppy, but it is an effective panacea for the situation
	public void callDerivedMethod(){
		if(myDynamicA instanceof DerivedClass){
			((DerivedClass)myDynamicA).moreWorkToDo();
		}
	}
}


Now you can treat ModularClass like 'A' with variable behavior, though the design can be much better such that this wouldn't be necessary.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You