Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

calling a method

 
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.
 
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.
 
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...
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic