This question already has answers here:
Calling super super class method
(12 answers)
Closed 7 years ago.
Let's say I have a base class called Vehicle, and another class called Car that extends it. Finally I have a class Luxury that extends Car.
I know I can use the keyword super to invoke a base-class method. How do I invoke a method of the Vehicle class from Luxury?
There is no builtin mechanism for this. You have to create a helper method in the first subclass.
public class A {
public void myMethod() { ... }
}
public class B extends A {
public void myMethod() {
// something
}
protected void myMethodA() {
super.myMethod();
}
}
public class C extends B {
public void myMethod() {
myMethodA();
}
}
Related
This question already has answers here:
Significance of inheriting method from superclass instead of default method from implementing interface in java 8
(2 answers)
Java8: Why is it forbidden to define a default method for a method from java.lang.Object
(5 answers)
Closed 1 year ago.
I have declared the default method and concrete method in the abstract class with the same name.
public class AbstractClassDefaultMethodTest {
public static void main(String[] args) {
DemoInterface testDefaultMethods=new TestDefaultMethods();
testDefaultMethods.display();
}
}
abstract class DemoClass{
public void display()
{
System.out.println("Inside display method of abstract class");
}
}
interface DemoInterface{
public default void display()
{
System.out.println("Inside display method of interface");
}
}
class TestDefaultMethods extends DemoClass implements DemoInterface
{
}
Why the concrete method of abstract class is getting called? Why we are not getting the compile time error, as both the method names are same ?
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
I have this code:
public interface Interface1{
void interfaceMethod1();
}
public class Class1<T extends Class0&Interface1>{
private T field;
public T getField(){
return field;
}
}
When I invoke class1.getField().interfaceMethod1(), where class1 is Class1 instance, I see error "Cannot resolve method".
I want to define class with generic field which will include methods from Class0 and Interface1.
Assume we have next definitions:
public interface Interface1 {
void interfaceMethod1();
}
public class Class0 {
}
public class Class2 extends Class0 implements Interface1 {
#Override
public void interfaceMethod1() {
}
}
public class Class1<T extends Class0 & Interface1> {
private T field;
public T getField() {
return field;
}
}
When you have
Class1 class1 = new Class1();
class1.getField().interfaceMethod1();
it is called type erasure. It means all generic arguments assumed to be Object, so class1.getField() return Object which lack of interfaceMethod1 method.
To fix that you should do this:
Class1<Class2> class1 = new Class1<>();
class1.getField().interfaceMethod1();
Now everything compiles fine.
This question already has answers here:
Can an abstract class have a constructor?
(22 answers)
Closed 5 years ago.
what's the pure virtual function?what's the constructor function?
public abstract class AmAbstract
{
private String name;
public AmAbstract(){
}
public AmAbstract(String name){
}
}
The constructors of concrete sub-classes of an abstract class still have to invoke the constructor of their super-class, even when its abstract.
That said, there is no reason for the abstract class constructor to have public access. protected access is sufficient.
public class Concrete extends AmAbstract
{
public Concrete () {
super (); // invoke the constructor of the abstract class
}
public Concrete (String name){
super (name); // invoke the constructor of the abstract class
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is super.super.method(); not allowed in Java?
If you have a class that derives from a class that derives from another is there anyway for me to call the super.super method and not the overridden one?
class A{
public void some(){}
}
class B extends A{
public void some(){}
}
class C extends B{
public void some(){I want to call A.some();}
}
See: Why is super.super.method(); not allowed in Java?
#tgamblin is right but here is a workaround :
class A{
public void some(){ sharedCode() }
public final void someFromSuper(){ sharedCode() }
private void sharedCode() { //code in A.some() }
}
class B extends A{
#Override
public void some(){}
}
class C extends B{
#Override
public void some(){
//I want to call A.some();
someFromSuper();
}
}
Create a second version of your method in A that is final (not overridable) and call it from C.
This is actually a poor design, but sometimes needed and used inside JDK itself.
Regards,
Stéphane
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there any way to forbid the son class to call the public method of super class in java?
some days ago, when I develop the plugin for hudson(a Continue integrate tool), I met the this
problem . I created the class which extends the SubversionSCM(the offical class). I just
wanted to override the public method of super class, then call super's public method back like
example. but the compile gave error.I don't konw why, how could it do?
the real situation is like following example.
public abstract class TObject{
abstract public void quark();
}
public class Animal extends TObject{
public void quark(){
System.out.println("this is the animal");
}
}
public class Dog extends Animal{
#overide
public void quark(){
System.out.println("this is the animal");
**super.quark();**
}
}
In this example, The Dog call the super.quark(); in it's quark method.
But I don't want the Dog could call super.quark();the Animal class is written by
others, so can't be changed.....
anyone who can help me?
As to your first question: if you were overriding something marked final (either the class or the method) the compiler would yield an error.
As to the second you could meddle with reflection. For instance if you were the author of TObject and did not want anybody to extend your class more than one node you could do something like this:
public class BaseClass {
public void apiMethod() {
if(check(0, getClass()) > 1) {
throw new IllegalStateException("Too much inheritance");
}
}
private int check(int prev, Class c) {
if(c.equals( BaseClass.class)) {
return prev;
}
else {
return check(++prev, c.getSuperClass());
}
}
}
But that would be sneaky...