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;
}
}
Related
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/
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.
This question already has answers here:
Get Concrete Class name from Abstract Class
(2 answers)
getting only name of the class Class.getName()
(7 answers)
Java - get the current class name?
(12 answers)
Closed 3 years ago.
I have an abstract class and i am wondering if it is possible to know the instance of that class inside one of its methods.
I mean if there's any way to get it. Like some java method like myClass.whereAmI() or something like that.
For example:
public abstract class MyClass {
public void myMethod(String string){
String instance = MyClass.getClass(); //I want to get the type of the instance.
....
}
}
Is this possible?
Just use the getClass() instance method inherited from java.lang.Object:
public void myMethod(String string) {
Class<?> instanceClass = getClass();
String instanceClassName = instanceClass.getName();
}
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
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.