Calling subroutines/methods from a class

N

newbie_at_java

Hi All,

I am quite new to the java language. But I have a lot of experience with the Perl language.

I'm trying to make something really simple work.
There are two files. One (named Sub.java) contains one method that prints one line and one (named Super.java) that must use Sub.java.
Using Eclipse, I've put those two pieces of code in seperate files in one package (as you can see) in one project.
Eclipse tells me: "The method show_sub() is undefined for the type Super". Why type ? Only class Super exists, no type Super?


I cannot make this simple feat to work. Please tell me, in simple terms, why this does not work? What do I do wrong?

Code for Sub.java:

package tips;

import static java.lang.System.out;

class Sub
{
void show_sub()
{
out.println("Method: sub is called");
}
}

Code for Super.java:

package tips;

class Super
{
public static void main (String args[])
{
show_sub();
}
}




--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 
I

Ingo R. Homann

Hi,

newbie_at_java said:
Code for Sub.java:

package tips;

import static java.lang.System.out;

class Sub
{
void show_sub()
{
out.println("Method: sub is called");
}
}

Code for Super.java:

package tips;

class Super
{
public static void main (String args[])
{
show_sub();
}
}

There are several (potential) problems:

Super and Sub do not have anything in common. Perhaps you meant "class
Sub extends Super"?

But even then, of course it, is not possible to call a method of the
subclass from within the superclass. (...if it is not already defined in
the super class and overridden in teh subclass, but we do not want to
make it too complicated).

Perhaps - completely different (and not what the names "Sub" and "Super"
indicate) - you want to create an Object of type Sub within the class
Super? Then, in the main-method you could say:

Sub sub=new Sub();
sub.show_sub();

or even shorter:

new Sub().show_sub();

Or - another possibility - is to just make everything static -
espoecially the method show_sub (just like you made 'main' static). Then
you can simply call it by this:

Sub.show_sub();

Even though, your question seems to indicate that you should read some
good tutorial about Java and expecially over "object oriented
programming in Java" if you wnt to do more with Java than such "Hello
World".

Ciao,
Ingo
 
I

Ian Wilson

newbie_at_java said:

Hello,

I'm no Java or OO expert so take the following comments with a pinch of
salt.
I am quite new to the java language. But I have a lot of experience
with the Perl language.

But have you done OO programming in Perl?
I'm trying to make something really simple work. There are two files.
One (named Sub.java) contains one method that prints one line and one
(named Super.java) that must use Sub.java.

Using the words "Sub" and "Super" suggests you are trying to use the
concepts of a class hierarchy where some classes are superclasses of
others (which are therefore subclasses of the former).

However your code looks like non-OO procedural logic divided into two
files. This is a bit like using Perl Modules in a procedural fashion.
Using Eclipse, I've put
those two pieces of code in seperate files in one package (as you can
see) in one project. Eclipse tells me: "The method show_sub() is
undefined for the type Super". Why type ? Only class Super exists, no
type Super?

The terminology can be confusing. You have not instantiated the class
"Super" by creating an object of that type.

If you wrote
Super foo = new Super();

Then foo would be an object of type Super. The object foo is an instance
of the Super class. Actually foo is really a reference to an object
which is an instance of class Super. You never really grapple with
objects directly, only indirectly through references to objects. This
trips people up a lot.

If your class 'Sub' was defined thus:
class Sub extends Super
then your choice of names would make a little more sense. However I
recommend you try something less abstract. The usual examples for
teaching include things like 'class Dog extends Mammal'.
I cannot make this simple feat to work. Please tell me, in simple
terms, why this does not work? What do I do wrong?

package tips;
import static java.lang.System.out;
class Sub {
void show_sub() {
out.println("Method: sub is called");
}
}

package tips;
class Super {
public static void main (String args[]) {
show_sub();
> }
}

Example 1:
==========
package tips;
import static java.lang.System.out;
class Sublime {
public static void show_sub() { // **** PUBLIC STATIC
out.println("Method: sub is called");
}
}

package tips;
class Superb {
public static void main (String args[]) {
Sublime.show_sub(); // *** Class method
}
}

Example 2
=========
package tips;
import static java.lang.System.out;
class Sublime {
public void show_sub() { // **** PUBLIC
out.println("Method: sub is called");
}
}

package tips;
class Superb {
public static void main (String args[]) {
Sublime sub = new Sublime();
sub.show_sub(); // *** Instance method
}
}

The second example is marginally more OO. I recommend you try to write
more like the second than the first example. When I started learning
Java (not very long ago) I also made the mistake of designing my
programs as if I was using a procedural language not an OO language.

I have not compiled or tested any of the above. It might all be rubbish.
Nevertheless I hope reading the above helps straighten out some concepts
for you.

It would be more usual to name show_sub as showSub. It makes programs
easier for others (in general) to understand if you stick to Sun's java
conventions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top