Actually my question was kind of answered already but I can't understand it properly. here is a code snippet:
public class Superclass {
public static void main (String[] args){
Superclass obj = new Subclass();
obj.doSomething(); #prints "from Superclass"
}
private void doSomething(){System.out.println("from Superclass");}
}
class Subclass extends Superclass {
private void doSomething(){System.out.println("from Subclass");}
}
It prints out : "from Superclass"
Since the reference type of the object obj is SuperClass, a call to
doSomething() tries to access the private method defined in SuperClass
itself (private methods cannot be overridden). As doSomething() is
accessible within SuperClass, the main method can call doSomething()
without giving any error/s.
As far as I can imagine the method calling works this way: (maybe I am totally wrong about this):
The instances of an object store only the data in themselves and when I call an instance method Java takes a look at the type of the reference variable and calls the method from the class of that type.
(It seems logical to me and explains why the code above prints "from Superclass")
But changing the access modifier in the first doSomething() method to protected confuses me, because I can't understand how Java knows there is an overriding if the reference type is the SuperClass and not the SubClass. Does overriding change the method in the Superclass as well?
Or am I totally wrong about how method calling and overriding works?
I would really appreciate If someone could help me out and explain what I am missing.
Private methods along with static methods and all variables cannot be overridden, they can be hidden. So you would need to change the access modifier of your method from private to any other access modifier.
N.B: There are a few protocols to follow when overriding a method; The access modifier of the method defined in the sub-class must have the same level of access if not more accessible than the method that is being overridden. Return types must be covariant etc...
The functionality of overriding a method is simply defining another way to execute the same method using inheritance.
No, overriding doesn't change at all the super class (unless you're doing some weird things with reflection, but that's another story).
The private keyword is used when only the class itself can "see" the method. On the other hand, the protected keyword is used exactly for inheritance: the class and it's childs can see the method. That's why you can only override protected methods.
Since you have defined "doSomething" as private method, your override did not work. Change it to public or protected and then see output.
To understand it better, move Subclass in another java file. Now tell me, can you make a private method call from an object handle?
I want my Widgets class's get method to extend that of the superclass org.powerbot.game.api.methods.Widgets, but I get a compilation error:
However, the superclass clearly has a matching get method:
static methods are not polymorphic in Java. In other words, they cannot be overriden.
The #Overide annotation is used to mark an overridden method. Static methods cannot be overridden in java. But you can redefine them. Just remove the annotation. You may optionally call the super class static method inside your sub class static method.
In Java Interface, we can have only final variables possible. We can also create static variables in Interface. But, at the same time we are not able to create static/final methods as Interface are only meant for Static Methods.
What is exactly the reason for not allowing static/final methods in Interface ?
A final method can't be overridden. That defies the purpose of having an interface if you cannot actually implement the method.
For the static part, see this question.
You got it wrong.
All variables are implicitly public static and final in interfaces.
Prior to Java 8, you can't create static methods in interfaces. All methods are instance methods.
Since the only goal of an interface is to have classes implementing them, and since methods in interfaces can't have any implementation, making them final would make no sense: they would have no implementation, and could not be overridden.
Interfaces are defined for instances, not statics.
"final" means "can't be overridden". That makes no sense for an interface whatsoever.
final means that it cannot be overriden.
static means that it can only be called using the class name. Since an interface will have multiple implementations, how will you know which implementation to choose since the interface cannot implement the method itself?
Because they are there in an interface to be implemented by some class. What would be the point of a method that can not have an implementation anywhere? (which is what final would suggest)
I have one more point to prove why interface methods can not be static :
interface MyInterface {
static void myStaticMethod();
}
Now let's have two classes are implementing "MyInterface"
// first class
class MyClass1 implements MyInterface {
static void myStaticMethod(){
// some implementation
}
}
// second class
class MyClass2 implements MyInterface {
static void myStaticMethod(){
// some implementation
}
}
Now I am instantiating like below:
1- MyInterface myObj1 = new MyClass1();
2- myObj1.myStaticMethod();
3- MyInterface myObj2 = new MyClass2();
4- myObj2.myStaticMethod();
// here at line 2 & 4 , it's wrong calling as myStaticMethod should be called using class name(because myStaticMethod is defined as static) like below:
MyInterface.myStaticMethod();--> But in this case,how to call different implementations of myStaticMethod() by MyClass1 & MyClass2 classes.
So it's proved that static can not be possible in interface method declaration.
For final ,it's quite clear that it will opposite to override functionality.
An interface is a pure abstract class. Hence, all methods in an interface are abtract, and must be implemented in the child classes. So, by extension, none of them can be declared as final.
Why Interface methods cannot be “static” & “final”?
All methods in an interface are explicitly abstract and hence you cannot define them as static or final because static or final methods cannot be abstract.
In the context of Java 8 and default methods, this question has a new meaning. static methods are now allowed, and why final methods still aren't possible is explained in this question.
1: we can't declare a final method ,because it contradicts it's rule as final method can't be override,but always need to define all the interface methods in it implemented classes.
2: we can't declare a static method ,because it contradicts it's rule as static method always needs the method body but we cant define any method inside a interface.
Well static methods work on classes and not instances so kind of strange/pointless. Having said that I've for one reason or another wanted this in some situations, though can't remember a case now so must have been long ago.
You can "work around" this though (rather alternative api design) as interfaces allow you to declare classes, so something like this:
interface MyInterface {
static class Helpers {
static void myStaticMethod(); //can be abstract etc as usual
}
}
You can subclass that class etc as normal of course, as well make it abstract, abstract methods etc etc.
We can not declare method of interface as static because method of interface instance method and we can not declare final because it is necessory to override method of interface in implemented class. for description check this link enter link description here
By default all the methods present inside an interface are public and abstract. If you declair a method as final inside an interface 1st of all you will get a compiler error and not even then it doesn't make any sense to have a final method because you will never be in a position to override it in child class.
In case of static even if Java allow in what so ever version it's not a good programming practice to use static inside an interface because for static methods u must have to provide the implementation which you must not provide inside an interface. Moreover, even if you provide the implementation inside an interface still u have to override it and then have to call it by the class name.
Interface cant have static method because if you know the static property that method declared static can be called without creating any object of class and sttaic methods are part of class not instance of class, so the answer is that how can you call abstract method till java 7, In java 8 you can declare method as static and call it by interface name dot method name.
Now answer fo final is that , final method is not overriden so how you will override it when class will inherit it
why can't we make Interface methods final?
because if you make a method final then you can not override it and the sole purpose of Interface is to have methods that will be overridden by all those class that implements that Interface.
why can't we make Interface methods static?
In Java 8 it's possible, you can make methods static but that method should have a method body
interface Test{
static void hello(){
System.out.println(“hello world”);
}
}
and you can access this method from a class implementing this Interface by
Test.hello();
I know in Java, static method can not be overriden by the subclass.
Two questions:
1. Why is that? Could anyone explain me the reason inside it?
2. Can subclass override final method in super class then?
Static methods aren't called on a particular instance - so they can't be called polymorphically. They are called on the type itself - nothing about the binding relies on any information which is only available at execution time. The point about polymorphic calls is that the method implementation which ends up being executed depends on the execution-time type of the target of the call; there's no target for static method calls, as such.
No, subclasses can't override final methods - the whole point of making a method final is to prevent it from being overridden.
why we should not use static and abstract for a single method?
the static keyword is defined so that a method can be called by a class name rather then an object. that means the method has to have some sort of definition. but abstract means you do not have any details about what the method does, it is as it says **Abstract**. When you inherit or extend a class you can then define the method.
Think of an interface.
If you are asking about having a static method inside of an abstract class, that is a different story. An abstract class is essentially as mentioned an interface and contains just a template of say functions that you must later on implement by inheriting / extending the class. Once you extend that class the static method does not come along with it (that is by default unless the access modifier is public / protected).
A static method is not inherited. Therefore, making it abstract is a nonsense.
The abstract keyword means that child classes must override the method - this is (one of the ways) Java supports polymorphism. If you want to make it so that subclasses cannot override the method you mark it final. So it would be impossible to have an "abstract final" method since they are the exact opposite of each other.
the static keyword implies final as well - all static method are also final. Thus it is impossible to have a method that is both static and abstract since you would be able to make a method that is abstract and final.
The reason for static being final is that it is bound to the class instead of the instance. That means that the compiler looks it up at compile time rather than runtime to determine which method to call. The reason what it is like that? Arbitrary decision that the designers of Java made - they could have allowed static method to be overridden but decided not to. I don't have any particular insight as to why the chose one over the other unfortunately.
As others have said, static+abstract is nonsense in Java. But there have been (rare) occasions where I've wished I could do just that.
The result I was looking for was basically to say that... "all concrete classes that extent this abstract class (or implement this interface) must provide a static method with this signature." This capability would allow these classes to provide meta-information about themselves.
Normally I have ended up with an instance method in these cases. If you stipulate that concrete implementations must support the default (no-arg) constructior, you can do...
MyInterface obj = MyClassThatImplementsMyInterface.newInstance();
obj.invokeTheMethodIWishWasBothStaticAndAbstract();