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

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.

Related

Is it not possible to invoke superclass method? [duplicate]

This question already has answers here:
Java How to call method of grand parents? [duplicate]
(3 answers)
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 3 months ago.
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in class A invoke the test()method defined in class C?
Answer is "It is not possible to invoke test()".
class C {
public static void test(){
System.out.println("IMESH ISURANGA");
}
}
class B extends C {
}
class A extends B {
//how prove it
}
class Demo {
public static void main(String[] args) {
//----
}
}
How to prove it?
The answer is "It is not possible to invoke test()". Because if it's possible, it would break encapsulation.
We can invoke the test() method in class B using "super." keyword. This happens under the assumption that we are aware that we are violating the encapsulation on our own class. But we are not allowed to access the class above the parent class using "super." keywords because we don't know what rules the superclass is enforcing. So, we can't bypass an implementation.

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/

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.

Java: Call a base super class method while skipping intermediate inherited super classes [duplicate]

This question already has answers here:
Calling super super class method
(12 answers)
Closed 8 years ago.
Let say I have three classes:
class A
{
public void method()
{ /* Code specific to A */ }
}
class B extends A
{
#Override
public void method()
{
/*Code specific to B*/
super.method();
}
}
class C extends B
{
#Override
public void method()
{ /* I want to use the code specific to A without using B */ }
}
The goal in this case is to use the code from A without using the code from B. I thought there was a way to do it by calling the super method, but this brings in the code from B as well.
Is there a way to do this?
The short answer is no. What you're seeing is that your design is flawed. It indicates that you need too move the code in class A out into a helper class. B and C would then use it via composition. You could try using partials to get the behavior you want. See this link for more details. Partial function application in Java 8
You could instantiate an object of class A in C and call the method method
class C extends B
{
#Override
public void method()
{ /* I want to use the code specific to A without using B */
A test = new A();
test.method();
}
}
Not sure if this is what you meant. Also asumed you forget the method name method in class A.
No there is no way to do it. You can simply create another method in class B that executes the code of A's method, and call that method in subclass C.

How does the compiler pick the right overriden method [duplicate]

This question already has answers here:
How does Java call object's function?
(5 answers)
How is dynamic binding implemented in Java?
(2 answers)
Closed 9 years ago.
public class Test
{
public static abstract class Node
{
private Node kid;
public abstract int getN();
public Node(Node kid) { this.kid = kid; }
public final Node copyWithNewChild(Node newKid)
{
return new Node(newKid)
{
public int getN()
{
return Node.this.getN(); //****
}
};
}
}
}
As you can see, I have this base class called Node, with a method getN() to be overriden by sub-classes.
Suppose I have a class called RedNode extending Node and providing a concrete implementation for getN(), and that I also have another class called SquareRedNode extending RedNodeand also providing an implementation for getN().
(And certainly, something else could also extend SquareRedNode (ie., the family tree could grow infinitely))
Now question, how does the compiler figure out which getN() to call when the copyWithNewChild() is executed?
(I'm not asking "what" implementation is being picked, which I can easily figure out by compiling/executing the code. I want to know how it is done.)
Thanks,
The compiler doesn't figure it out, it's a virtual method and it doesn't get resolved until runtime.
The JVM looks at the class of the object it's calling getN on and sees if it implements getN. If it does, it uses it, otherwise it looks at the superclass and sees if it implements it. It proceeds up the class hierarchy this way until it finds an implementation.

Categories

Resources