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

Recommended Answers

All 13 Replies

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

make the methods and fields public in the master class?

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?

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

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.

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?

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

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

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

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

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

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.

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.

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.