webdevRefinery Forum: Why can't I do something like this? - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 07 May 2012 - 03:28 PM (#1)

Why can't I do something like this?


Making a 'print' class for personal use
In my print class I have this:
Posted Image

Why can't I do that, yet I can do this:
Posted Image
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 07 May 2012 - 04:17 PM (#2)

Add @Override notation?
Reserved.
0


User is offline Qasim 

  • Group: Members
  • Posts: 520
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 07 May 2012 - 04:17 PM (#3)

Sometimes IDE's just make weird errors like that, don't they? I'm not too sure but it might also be because your out classes have different paramters yet your in class have the same parameters.
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 07 May 2012 - 04:41 PM (#4)

Pretty sure just overriding it will work perfectly. That, or just redefine the in method.
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 07 May 2012 - 05:29 PM (#5)

View PostQasim, on 07 May 2012 - 04:17 PM, said:

Sometimes IDE's just make weird errors like that, don't they? I'm not too sure but it might also be because your out classes have different paramters yet your in class have the same parameters.

Nope, if an IDE for a compiled language complains, it means the compiler is complaining. Eclipse uses JRuby to instantly compile your code and see if the compiler complains.
Reserved.
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 08 May 2012 - 08:54 AM (#6)

The important factors of a Java method declaration are the name and parameter types. Even though both
in
methods return different types, they have the same name and have the same parameter types, so the Java compiler does not know the difference between the two.

I recommend doing it like this:
/**
 * Test class used as an example for Quinn.
 */
public class Test {
    Scanner scanner;
    
    /**
     * Constructor initializes the scanner to system input.
     */
    public Test() {
        this.scanner = new Scanner(System.in);
    }

    /**
     * Reads the next integer from the scanner with a prompt.
     * @param prompt the prompt to ask the user
     */
    public int inputInt(String prompt) {
        out(prompt);
        return this.scanner.nextInt();
    }

    /**
     * Reads the next string from the scanner with a prompt.
     * @param prompt the prompt to ask the user
     */
    public String inputString(String string) {
        out(string);
        return this.scanner.next();
    }

    /**
     * Shortcut to the System.out.print.
     */
    public void print(Object object) {
        System.out.println(object.toString());
    }
}


Differences:
  • javadoc comments
  • generic "Object" parameter for print; may not work with primitives, so you'll have to overload it
  • differently named input methods (just like the Scanner class)


Hope it helps (and compiles because I didn't test it.)
reputation += 1 if post.is_helpful else 0
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 May 2012 - 09:40 AM (#7)

View Postianonavy, on 08 May 2012 - 08:54 AM, said:

The important factors of a Java method declaration are the name and parameter types. Even though both
in
methods return different types, they have the same name and have the same parameter types, so the Java compiler does not know the difference between the two.

ack didn't see that, but yup +1.

Java method signatures are method name + params.
Reserved.
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 08 May 2012 - 11:05 AM (#8)

Yeah, you have to have either a different name or different arguments. The reason being that Java won't know what method you're calling otherwise. For example, you could do something like this

public Integer someFunc(int a, int B)
{
    //do something
}

public String someFunc(int a, int B)
{
    //do something
}

public static void main(String[] args)
{
   Object myVar = someFunc(1, 2);
}


Which function are you calling? Technically there's 2 functions which match your call, and both of them return Objects...
1


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 08 May 2012 - 02:30 PM (#9)

I realized what Sephern said after a thought about it a little more, and later went with the same idea that Ianonavy had.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 08 May 2012 - 03:34 PM (#10)

View PostThatRailsGuy, on 08 May 2012 - 09:40 AM, said:

ack didn't see that, but yup +1.

Java method signatures are method name + params.

Off topic: You always say +1 but never actually give me reputation. xD I've noticed that on multiple occasions now.
reputation += 1 if post.is_helpful else 0
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 May 2012 - 03:44 PM (#11)

View Postianonavy, on 08 May 2012 - 03:34 PM, said:

Off topic: You always say +1 but never actually give me reputation. xD I've noticed that on multiple occasions now.

Sometimes I do, sometimes I don't :P Generally if I say something I don't.
Reserved.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services