Skip to main content
gamedev.net
🔒 Locked

[java] Vice versa

Started by seppi Dec 19, 2001 at 10:39 AM 1 replies 350+ views
Original Post
seppi
seppi
If I do a creature of Class A, which creates a creature of class B, is it possible to call methods in class A with this B creature? Sorry my poor english Java knowledge, I couldn't say it better :D Edited by - seppi on December 19, 2001 11:40:19 AM
Wrathnut
Wrathnut
Seems like the same tpye of inheritance questions over and over.

As long as Class B extends Class A, Class B will have all of the methods of Class A. Just remember in the constructor for Class B to call the constructor for Class A right away.

Example

  
public Class A{

public A(){ //Constructor for A

//Init class A

}


public void doSomething(){//A method of class A


}

}

public Class B extends A{

public B(){
super();
//Now do init for class b

}


}



Now when you create an object of Class B, it should have the method do something. B.doSomething();

Hope that helps.

War doesn''t determine who is right, war determines who is left.
stefu
stefu
I think he meant the following:

''creature'' = ''object''? :D

  

public Class A
{
void TestMain()
{
B b = new B();
b.SetA( this ); /* give ''this'' - reference to B */
b.TempB();
}

void TempA()
{
}
}

public Class B
{
private A reference;
public void SetA(A ref) { reference = ref; }

public void TempB()
{
/* now you can call A here */

reference.TempA();
}

}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.