0

Here is the C++ code

public class First {
 public: 
     virtual int firstmethod(int a, int b) = 0;
     virtual int secondmethod(int a, int b, int c) = 0;
}

public class Second : public First {
 public:
    int firstmethod(int a, int b) {
        int result = a * b;
        return result + 3;
    }
}

Here is what i have for Java so far

public class First {
  static in firstmethod(int a, int b) = 0;
  static int secondmethod(int a, int b, int c) = 0;
}


public class Second extends First
{
   static int firstmethod(int a, int b)
   {
     int result = a * b;
     return result + 3;
   }
}

Is this right? EDIT i edited the question to make it more clear and easier to follow

dougle
  • 95
  • 1
  • 1
  • 6
  • 2
    I'm not sure if translating from C++ to Java will work if the C++ source is wrong already - you are seamlessly mixing features from both languages here (C++, for example, knows no access specifiers on classes). – Alexander Gessler Apr 25 '11 at 01:31
  • Im just trying to write equivalent java code for the C++ code and I'm not familiar with the virtual function or the public: – dougle Apr 25 '11 at 01:34
  • you should not edit the question in such a way that comments and answers do not fit anymore. At least mark your question clearly as edited, if you need to do so. – tanascius Apr 27 '11 at 08:54

4 Answers4

3

No, not at all. Static methods are never virtual, and Java doesn't use "=0" for pure virtual methods (it uses the "abstract" keyword.) Classes that have abstract methods must themselves be marked abstract. Also Java methods aren't public by default -- each method must be marked "public" individually.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
3

It would be something like :

public class Whatever {
  public int mymethod(int one, int two) { return 0; }
  public int myothermethod(int one, int two, int three) { return 0; }
}


public class However extends Whatever
{
   @Override // optional annotation
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
}

But then you could instanciate Whatever. To prevent instanciation of Whatever, either mark it as a abstract or make an interface out of it. It all depends how you want your classes to inherit Whatever. Since there cannot be multiple inheritance, choose wisely.

public interface Whatever {
   public int mymethod(int one, int two);
   public int myothermethod(int one, int two, int three);
}

public class However implements Whatever
{
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
   public int myothermethod(int one, int two, int three) {
     return 0;
   }
}

or

public abstract class Whatever {
   public abstract int mymethod(int one, int two);
   public abstract int myothermethod(int one, int two, int three);
}

public class However extends Whatever
{
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
   public int myothermethod(int one, int two, int three) {
     return 0;
   }
}

** EDIT **

After some enlightenment from the comments, your C++ to Java equivalent is actually the third construct since you're using virtual class methods on your C++ code.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
1

I would say for your java translation you're looking for:

public interface Whatever {

     public static int myMethod(int one, int two);
     public static int myOtherMethod(int one, int two, int three);

}

public class However implements Whatever {
     public static int myMethod(int one, int two) {
          int answer = one * two;
          return answer + 3;
     }
     public static int myOtherMethod(int one, int two, int three) {
          int answer = one * two;
          return answer + 3;
     }
}

Also, just for the sake of clarity and convention, I would be wary of naming variables 'one' or 'two' as this might lead to confusion.

Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
  • this is invalid Java and it won't compile; you can't have a `static abstract` method and, anyway, your methods are not declared as such and do not contain a body. (for the record, I did not -1 your answer) – Yanick Rochon Apr 25 '11 at 01:41
  • read http://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface – Yanick Rochon Apr 25 '11 at 01:53
1

Here's my translation:

public abstract class Whatever {
    public abstract int mymethod(int one, int two);
    public abstract int myothermethod(int one, int two, int three);
}

public class However extends Whatever {
    @Override
    public int mymethod(int one, int two) {
        int answer = one * two;
        return answer + 3;
    }

    @Override
    public int myothermethod(int one, int two, int three) {
        return ...;
    }
}

I also like Yanick's answer about using interfaces; that's a better approach. I'm keeping my answer because of the use of @Override, which is useful for Java code.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435