Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling a method

 
renu yella
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java.Trying to learn myself. I have created 2 different classes (let me say class1 and class2)from which one class in inherited.I have created an object , set and get methods(like settotal(),getcode()) in class1. I inherited class in class2 . in class to i have given few other set and get methods. I even wrote toString method in class2 to call all the methods. I am able to call the methods only from class2 but not from class1. it is showing an error as cannot resolve symbol method getcode();

I wonder how to call the methods from class1 to class2. My main class is again a different class.

plz help me out.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, renu!

Could you post some example code that creates the error message you're getting?

When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
Shashank Agarwal
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you are not able to call class1 from class2 because class1 has inherited class2's methods, but class1 has'nt. do this -

lets assume Class1 is extending Class2

public class Class2
{
Class2()
{
Class1 c1 = new Class1();
c1.myMethod2();
}

public void myMethod1()
{
System.out.println("My Method 1 Called");
}
}

class Class1 extends Class2
{
Class2()
{
myMethod1();
}

public void myMethod2()
{
System.out.println("My Method 2 called");
}
}

Understood how class1 can now call methods in class2 and class2 in class1? I hope u did...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic