When do I need to use #Override, after implemented abstract method [duplicate] - java

This question already has answers here:
When do you use Java's #Override annotation and why?
(27 answers)
Closed 2 years ago.
public interface Game{
public abstract boolean isValid(int coup);
}
public Machin implements Game{
//Do I need to write #Override here ?
public boolean isValid(int coup){
//exemple
if (coup==0){
return false
}
return true
}
}
I don't understand Overriding with abstract method could you help me ? Do I need to put #Override ?

Overriding is when a subclass method is different than that of its superclass. Interfaces do not have method bodies - they solely force all classes that implement to have these functions. You do not need the override tag in the Machin class but if you were to create a subclass of with an isValid() method, you would need to override.

Related

Java Interface two method with optional parameters [duplicate]

This question already has answers here:
Why should I ever overload methods?
(6 answers)
Closed 2 years ago.
I want to have an interface that allows me to use methods with optional parameters. Suppose I have an interface:
public interface Stuff {
public int Add();
}
And I have two classes A and B who implement the interface. One method needs parameter, but the other one doesn't.
public class CLASS A implements Stuff{
public int Add();
}
public class CLASS B implements Stuff{
public int Add(String name);
}
How can I achieve this?
I think You have to override function.
You can read about that here => https://www.geeksforgeeks.org/overriding-in-java/

can two methods have different return type in method overriding in java [duplicate]

This question already has answers here:
Overriding a method with different return types in java?
(7 answers)
Closed 8 years ago.
I want to ask that in method overriding , can the method have different return type..
For eg.
class A{
int x(){
System.out.print("1");
}
}
class B extends A{
void x(){
System.out.print("2");
}
}
class C{
public static void main(){
A a = new B();
a.x();
}
}
What will be the o/p and whether it is called overriding.
Actually, you sorta can but in a limited fashion. A child class may return a sub-class of the class returned by the parent.
class A{
Object x(){
System.out.print("1");
}
}
class B extends A{
#Override
String x(){
System.out.print("2");
}
}
Other than this sub-class relationship b/w the return types, No.
You cannot do what you're specifying here, as you will get a compiler error specifying that your method you're attempting to override clashes with the new definition, using an incompatible return type.
In order to override a method, you need to have the same method signature. You can use the #Override annotation to test this.
No different return type isn't method overriding, verify by adding #Override annotation

Java - Invoke selective super class method possible? [duplicate]

This question already has answers here:
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 8 years ago.
I am using inherited codes which cannot be modified. It is being overrided many times. I want to invoke a specific overrided method of a super class (not a direct super class).
public class SuperSuperClass
{
...
public doSomething()
{
//Does something that I want
}
}
public class SuperClass extends SuperSuperClass
{
...
public doSomething()
{
//Does something I do not want
super.doSomething();
}
}
public class SubClass extends SuperClass
{
...
public doSomething()
{
SuperSuperClass.doSomething(); // is this possible?
}
}
The SuperClass.doSomething() does something I do not want before itself calling SuperSuperClass.doSomething(). Is there a way I can invoke SuperSuperClass.doSomething() from SubClass?
The answer to your question is "no". In Java there is no way to express that you want to invoke an instance method that was defined in some specific class that is part of your inheritance hierarchy. The language provides no way to express that.

Returning instance of abstract method [duplicate]

This question already has answers here:
Java generics - overriding an abstract method and having return type of the subclass
(3 answers)
Closed 9 years ago.
I have an abstract class with a method that i would like to return 'this' of the classes' subclasses. When i'm using the method i don't want it to return the object casted as MyClass and me have to recast it back to what it really is. Is there a pretty way of doing this with a generic?
abstract public class MyClass{
public <aSubClassOfMyClass> doSomething(){
return this;
}
}
You can make the class generic using the CRTP:
abstract public class MyClass<T extends MyClass<T>>{
public T doSomething(){
return this;
}
}

Java interfaces reduction in visibility, only NOT [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reduce visibility when implementing interface in Java
I must be missing something obvious, but i am getting:
Cannot reduce the visibility of the inherited method
And I don't see how. This is my interface:
interface QueryBuilderPart {
StringBuilder toStringBuilder();
}
And this is my implementation:
public class Stupid implements QueryBuilderPart {
#Override
StringBuilder toStringBuilder() {
return null;
}
}
Both the class and the implementation are in the same package. Any Ideas?
By default interface's method is public, but you reduce it to default visibility, which is package level visibility.
So the following two block of code are the same:
interface QueryBuilderPart {
StringBuilder toStringBuilder();
}
interface QueryBuilderPart {
public abstract StringBuilder toStringBuilder();
}
Note that interface's method is abstract as well
So you should do as following:
public class Stupid implements QueryBuilderPart {
#Override
public StringBuilder toStringBuilder() {
return null;
}
}

Categories

Resources