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
Related
(newbie in Java) I couldn't find exactly this question on SO. I have project, with two files (phseudo-code):
First Java File (class)
public class A {
public void xyz() { System.out.println("hello");}
}
Second Java File (class)
public class B Extends ZZZZZ {
public void callme() {
xyz(); // <----------------- I want to call in this way, but It cant be done like this.
}
}
How to make xyz() to call successfully (like as if was defined inside b() class natively !!).
p.s. again, I don't want to call it with classname in front, like this:
a.xyz();
The whole idea of instance methods, like xyz is in this, is that you are using the state of an instance of A in the method, without having to pass that instance as an argument like this:
... String xyz(A thisInstance, ...) {...}
Instead you use:
A thisInstance = ...;
thisInstance.xyz(...);
That's why you need an instance of A, because it is practically an argument to the function.
However, if you don't need an instance of A, you can make the method static:
static String xyz(...) {...}
Then you can call it without passing an instance of A:
A.xyz(...);
You can use a static import so that you don't have to write A:
import static A.xyz;
...
xyz(...);
Okay several possibilities:
Instantiate A:
A a=new A();
a.xyz();
(you do not want this)
Heredity:
public class B extends A {...}
and
public class A extends ZZZZZ{...}
so you can still extend ZZZZZ;
Interface:
public interface A{...}
public class B extends ZZZZZ implements A{...}
Static Method:
public class A{
public static void xyz()
{
System.out.println("hello");
}
}
public class B{
public void callme()
{
A.xyz());
}
}
This will help you.
class A {
public void xyz() {
System.out.println("hello");
}
}
class ZZZZZ extends A{
}
class B extends ZZZZZ {
public void callme() {
xyz();// <----------------- calling this shows error
}
}
I think we can always replace class with multiple interfaces e.g.:
public class C implements A,B{
}
into another version that each class contains at most one interface ,is it true (I just want to ask "If we can...", not "If we should..." here)?
Consider C which overrides methods a() from A, and b() B:
public class C implements A,B{
#override
public void a(){
}
#override
public void b(){
}
}
it is volating single responsibility principle because either change a() or b() requires change C, instead we can wrap A and B with an independent class:
public class ConcreteA implements A{
#override
public void a(){
}
}
public class ConcreteB implement B{
#override
public void b(){
}
}
public class C{
ConcreteA a;
ConcreteB b;
}
is it true?
And even your class has a single responsibility which requires 2 interface, it is still possible to rewrite it to become one interface only in each class,e.g.,the original version:
public class C implements A,B{
#Override
public void a(){
}
#Override
public void b(){
a();
}
}
in this case we can rewrite it as:
public class ConcreteA implements A{
#Override
public void a(){
}
}
public class C implements B{
ConcreteA a;
#Override
public void b(){
a.a();
}
}
So my question is, is it true that we can replace all of our classes that contain more than 2 interfaces into the version that contains at most one interface only?
if not, at what situations I need to implement more than one interface to finish the task?
The question whether implementing multiple interfaces or using multiple inheritance in general violates the single responsibility principle has been discussed before, for example here. There seems to be no clear consensus about it. To my opinion, the definition of SRP you are using is too strict.
The Serializable interface mentioned in the comments is a special case because it is a marker interface that does not declare any methods. But there are other interfaces like Runnable that usually make no sense to be used alone.
Coming back to your question: Yes, in theory it is possible but not without breaking other important principles like DRY (don't repeat yourself). Just think of a class that not only implements methods declared in the implemented interfaces but also its own method:
public class C implements A,B {
public void a(){}
public void b(){}
public void c(){}
}
Splitting results in:
public class CA implements A {
public void a(){}
public void c(){}
}
public class CB implements B {
public void b(){}
public void c(){}
}
Now method c()is duplicated. Yes, you could use an additional interface C or put this method into some kind of utility class, but I'd say that this will typically not improve the design.
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();
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Abstract method's property is that it doesn't have body, and so when concrete class extends abstract class, it must implement that abstract method.
So my question is how can an abstract method can be overridden when it doesn't have a body? Isn't it implemented, not overridden?
Abstract class with an abstract method.
The method is also marked with abstract and has no body.
public abstract class A {
public abstract void method();
}
Concretization in the class B that extends abstract class A. Also, the method loses the abstract keyword and gets the body.
public class B extends A {
#Override
public void method() {
System.out.println("Hello");
}
}
public abstract class BaseClass {
public abstract void doSomething();
}
public class ConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hi!");
}
}
public class AnotherConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hello!");
}
}
The implementation of the doSomething method that gets executed will depend on the runtime type of the object you are calling doSomething upon.
Just by providing an implementation for the method , provide it with a body.
public abstract class Base {
public abstract void someMethod(); // no implementation here
}
public class Sub extends Base {
#Override
public void someMethod() {
//It is Overridden
}
}
public abstract class A {
public abstract String getValue();
}
public class B extends A {
#Override
public String getValue(){
return "I'm Overriden";
}
}
It's the same like in any other class.
When you are implementing a superclass method in subclass, it will override. So Even if the superclass method is abstract, if you implement it in subclass, then it will override it.
So,
public class SuperClass{
public abstract void someMethod();
}
public class SubClass{
public void someMethod(){
System.out.println("subclass method");
}
}
Foo.java
public abstract class Foo {
public abstract void doStuff();
}
Bar.java
public class Bar extends Foo {
#Override
public void doStuff() {
// doStuff
}
}
Abstract method can be overridden just like overriding other normal methods, by providing an implementation.
This is the same as any other method override:
public abstract class AbstractClass {
public abstract void abstractMethod();
}
...
public class ConcreteClass {
#Override
public void abstractMethod() {
....
}
}
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...