This question already has answers here:
Unable to #Override the compareTo() method in Java
(5 answers)
Closed 4 years ago.
I have simple class with 2 fields. I want to be able to sort objects of this class so I implement a compareTo() method. The field I want to use for sorting is an int. So my method is pretty simple ..
public int compareTo(macToPortMapping pnum) {
// we want to be able to sort a mapping based upon the port number
return this.portNo - pnum.portNo;
}
This seems consistent with everything I've read about implementing a comparable. Still the netbeans wants to remove #Override and my compile message "method does not override a method from a supertype"..
This doesn't mean anything to me. Any pointers?
Well, I found a solution --
in my class declaration I had to include an implements..
public class macToPortMapping implements Comparable<macToPortMapping>{
int portNo;
macAddress addr;
}
when I put that on the class, it all worked as expected. I don't know how I missed that in the reading.
Related
This question already has answers here:
Why should I use the keyword "final" on a method parameter in Java?
(12 answers)
Closed 1 year ago.
I have a look at several questions e.g. In Java, should I use “final” for parameters and locals even when I don't have to? on SO and I am a little bit confused after reading the suggestions and answers.
In the project that I ma working on, there are lots of final keywords for the method parameters in methods and interfaced as shown below:
interface:
MenuDTO findMenu(final UUID menuUuid);
implementation:
#Override
public MenuDTO findMenu(final UUID menuUuid) {
}
As far as I know, using final keyword for method parameters as shown above is pointless. So, should I remove the final keywords from the interface methods and their implementations?
I also see no meaning in using final in parameters in Java. The final means you cant cant reassign value to variable. This you cant reassign anyway since the value is parameter. It looks for me like this style of writing final is coming from someone who worked with C++ before Java.
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
When to use an interface instead of an abstract class and vice versa?
(26 answers)
Closed 2 years ago.
I'm trying to wrap my head around interfaces and I've been reading a lot of similar questions/answers on the topic, but it's still not clicking with me. I understand what an interface literally is, but I don't get why you would ever use it.
So an interface contains a bunch of empty functions, and you then implement that interface in a class. But what I don't get is, you still have to write a full function declaration for any function you take from the interface. So say you have a function 'void printHello()' in your interface, and you implement that interface in a class. You still have to write:
public void printHello() {
System.out.println("Hello!");
}
which is a complete function declaration. You could delete the 'implements interface' command and literally nothing would change, you would still have a working function 'printHello()'. So functionally speaking, isn't the interface basically doing nothing? To go further, you could have another class that also implements the same interface, but you could make the same function do something completely different:
public void printHello() {
System.out.println("Bababooey!");
}
So it's not like it's pre-defining functions so you can call them anywhere like a class/object would do. If this is the case what is the interface really providing?
By using interfaces you outsource the responsibility of logic implementation to the classes which implements that interfaces, this is called API.
Lets say you have a List which is an interface and which is implemented by a bunch of different classes like LinkedList or ArrayList. By using List you are flexible to utilize any implementation of that List which varies on your particular needs and simply switch to a different one later on, whereas you code remains the same -- you still use List. So doing an add method you are not interested or you don't care how it is done, you just want to add an element to the collection and how it is implemented internally it is not your business.
This question already has answers here:
How are Anonymous inner classes used in Java?
(18 answers)
Closed 5 years ago.
I have found a this example with the following piece of code:
CompletableFuture<T> completable = new CompletableFuture<T>() {
#Override
public boolean cancel(boolean mayInterruptIfRunning) {
// propagate cancel to the listenable future
boolean result = listenableFuture.cancel(mayInterruptIfRunning);
super.cancel(mayInterruptIfRunning);
return result;
}
};
What does this syntax means? Is this method overriding a method in the class without extending it?
This block of code creates an anonymous subclass of CompletableFuture. This anonymous subclass overrides the cancel(bool) method with a new implementation. An anonymous subclass is equivalent to creating a new class that extends CompleteableFuture with the caveat that it isn't straightforward to re-use this new subclass somewhere else.
#Override is not strictly necessary in this case but it provides you several benefits, as described by the answer to this question:
Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.
Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like #Implements), but it's better than nothing.
It's an anonymous class (one of the nested class types). Please also refer to the Java tutorial - Classes and Objects for further class types, if you are interested in them.
This question already has an answer here:
Why won't my public void Constructor {} compile?
(1 answer)
Closed 6 years ago.
Hello. While trying to compile a simple Java class, i have found that i get an error that tells me the "actual" and "formal" arguemnt lists differ in length. However, i've only made constructors for the "Prosessor" class that are of argument length 2, and I'm only trying to construct it using argument lengths of 2 as well. Does anyone know what my mistake could be?
You are defining a method, not a constructor.
A constructor has no return type. Remove the void keyword.
Because you haven't defined any constructors, your class currently has the default, zero-parameter constructor, so your use of a two-parameter constructor is invalid.
This question already has answers here:
Why doesn't Java allow enum to be defined within a method? [duplicate]
(3 answers)
Closed 8 years ago.
Java does not allow enum to be declared in a method that is a basic java syntax.
But
Could any body explain why is that, what could have gone wrong if it would have been allowed by Java, I am sure there must be some cause behind this restriction, any idea?
class Example {
void aMethod() {
//This is not allowed
enum Status {
NEW,
PROCESSING,
COMPLETED;
}
}
}
enum types are typically used to share constant values between classes so declaring them in the scope of a method wouldnt make any sense