Java Interface two method with optional parameters [duplicate] - java

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/

Related

Why does the code can instantiate a java interface [duplicate]

This question already has answers here:
Java: Interface with new keyword how is that possible?
(9 answers)
Closed 8 months ago.
I understood that java interface cannot be instantiated. However, the below code looks like it instantiated an interface ? I'm trying to understand the code.
interface I{
int a();
int b();
}
public class C {
public static void main(String [] arg){
I i=(new I(){
public int a(){return 1;}
public int b(){return 2;}
});
assert i.a()==1 && i.b()==2;
}
}
An interface is like an all-abstract class, but that unambiguously supports multiple inheritance. You can instantiate any child of it that implements all the abstract methods.
The code you are asking about is creating an anonymous class that provides an implementation of all the abstract methods. Since the class is anonymous, the type of its instance can is most specifically given as I. Given that it's anonymous, that's also the only aspect of it you care about.

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

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.

Overriding method while creating an object [duplicate]

This question already has answers here:
Java - Interface, instantiating an interface?
(3 answers)
Closed 7 years ago.
I came across a piece of code that I don't entirely understand.
There is an interface that is created within a class like this:
public class SomeClass {
public interface SomeInterface{
public String getInfo(String str1, String str2);
}
public static final SomeInterface SomeOtherName = new SomeInterface() {
#Override
public String getInfo(String str1, String str2){
//return something;
}
}
}
and then in another class they call the method getInfo using SomeOtherName. I don't understand what is going on here. To be honest I have not created an interface while in a class and then create an object of that interface type and then override methods in it. Can someone please explain me what is going on in this piece of code as I need to test it.
They are called as annonymous classes
They enable you to declare and instantiate a class at the same time.
Edit :
They are like local classes except that they do not have a name. Use them if you need to use a local class only once.
SomeOtherName is an instance of an anonymous class that implements the SomeInterface interface.

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.

why Java class can extends only one class but implements many interfaces? [duplicate]

This question already has answers here:
Why is Multiple Inheritance not allowed in Java or C#?
(17 answers)
Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
(21 answers)
Closed 9 years ago.
In C++, you can extends many classes, so what's the advantages of this design in Java that a class can only extends one class ?
Since interface is a pure kind of class(abstract class actually), why not limit the number of interfaces implementation just like class extension ?
Being able to extend only one base class is one way of solving the diamond problem. This is a problem which occurs when a class extends two base classes which both implement the same method - how do you know which one to call?
A.java:
public class A {
public int getValue() { return 0; }
}
B.java:
public class B {
public int getValue() { return 1; }
}
C.java:
public class C extends A, B {
public int doStuff() {
return super.getValue(); // Which superclass method is called?
}
}
Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.

Categories

Resources