Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Calling subroutines/methods from a class

Thread view: 
newbie_at_java - 03 Apr 2007 10:45 GMT
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();
 }
}
Ingo R. Homann - 03 Apr 2007 11:00 GMT
Hi,

> Code for Sub.java:
>
[quoted text clipped - 21 lines]
>   }
> }

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
Ian Wilson - 03 Apr 2007 15:35 GMT
> Hi All,

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?
[quoted text clipped - 13 lines]
>     }
> }

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.