This is a very naiveish question, but here goes:
An overriden method from a base class will mean that calls to the sub class will call the derived, overriden method, correct?
Thus, if there is no override annotation, the method in the base class will be called. So the override method will serve purely to document the intent - call one version of a method over the other.
Is this the case?
This leads me to the following question:
What is the difference between an abstract class which 5-6 classes may derive from but the methods inherited in the derived classes are not overriden, and one class (Static or not being irrelevant), used by those 5-6 classes?
The #Override annotation is intended ONLY to catch errors at compilation time. It does not affect override behavior at runtime. The idea is that you give the compiler the chance to inform you that your method name or signature is wrong.
The override annotation is simply a marker to show the the method overrides a superclass method.
It being there has no effect at runtime. See here:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Override.html
Using abstract is an architectural design to ensure that users either must implement an abstract method (declared but not implemented in the base class) or that they all have access to the same base methods and data (this is the same as the latter example you gave).
Thus, if there is no override
annotation, the method in the base
class will be called.
No. The overriding method in the derived class will always be called.
So the override
method will serve purely to document
the intent - call one version of a
method over the other.
Is this the case?
No. As per the other answers, it tells the compiler to insist that there was something to override.
Related
Say I have an abstract class called "Subsystem.java" and I have a class that extends it called "Drive.java". In Subsystem.java, I have a method called initialize with no arguments. How do I Override that method in Drive.java and add arguments? In other words, I want to be able to override a method with no arguments into one that has arguments. Is this even possible? Thanks.
No, that is not an override, but an overload. You still couldn't create an instance from Drive, since it would have an unimplemented abstract method.
Overrides have the exact same signatures.
Overloads have the same name, but different parameter types.
You can not override as like you describe.
When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
you can refer below link for more information about overriding a method in Java.
Method overriding in Java
or you can also refer Oracle Java documentation
Oracle Java document
This question already has answers here:
When do you use Java's #Override annotation and why?
(27 answers)
Closed 9 years ago.
This is a newbie question.
I read that JVM's execution starts from searching for the methodname from lowest class in the hierarchy and if the method is not available in that class it traverses to the parent class looking for the method.
If this is the case then why do we need to use "#override" for adding custom logic to the inherited class ?
The below example illustrates my question
class superclassA
{
method()
{
}
}
class subclassB extends superclassA
{
#Override
//While executing if JVM starts looking for the method name from the lowest hierarchy
//then why do we have to use "override" as the methodname will be matched from the lowest level itself?
method()
{
--custom subclass specific code...
}
}
If this is the case then why do we need to use "#override" for adding custom logic to the inherited class?
We don't. The #Override annotation has no technical meaning - it exists to document the fact that this method overrides one in the superclass, which has some advantages:
If you look at the code, it tells you there is an superclass method that might be important to understand what this method does
You will get a compiler error if the superclass method's signature changes in a way that the subclass method does in fact not override it anymore.
You can get a compiler warning if you override a method without using the annotation, in case you do it inadvertantly.
#Override simply helps Java compiler detect errors in source code, compilers are generate an error if a method annotated with #Override does not override it in fact.
It is not mandatory to annotate a method that overrides a supertype methods with #Override.
You don't need #Override. But it's a useful annotation that causes the compiler to check whether or not you are really overriding the method that you say you are. When you #Override a method that is not actually overriding a method, the compiler will inform you of this discrepancy. Additionally, it just makes your code more clear: since all methods in Java are implicitly virtual, and a method in a derived class with the same signature as that of a non-final method in a super class implicitly overrides it1, adding #Override makes the code easier for humans to understand.
1: To be clear, you can not have a method in a derived class with the same signature as a final method in a super class.
What is the difference between abstract methodand method overriding in java? Because same result can be found by using method overriding. so what is the necessity of abstract method.
An abstract method forces a programmer who inherits the method to define an implementation for it, i.e. the class says "I will need an implementation of this function, which the concrete implementation of this class must provide".
This is in contrast to overriden methods, which allow (rather than require) the inheriting class to change the implementation of the method for objects of that class.
In particular, overridable methods (or "virtual" methods) have a basic implementation (which can itself be empty) that the overriding method can call.
The abstract method needs to be implemented in order for the subclass to be utilized. You must also implement non-abstract methods, for instance, in the case of interface method signatures.
I think it is correct to say that you implement abstract methods in the same way that you implement method stubs of an interface. Overriding is not the same as implementing because you can override methods which are not stubs. Subclasses often override methods where the superclass is neither an interface nor an abstract class. So the term overriding has a broader meaning than implementation.
You may find this tutorial helpful as it has quite a clear definition.
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Hope that helps!
The two have different usages which can't be compared.
An Abstract Class is created only to be sub classed by some other class. The implementing class necessarily has to implement all the abstract methods declared inside it. However Method Overriding is the ability of a subclass to override a method from an inherited superclass whose behavior is "close enough" and then to modify behavior as needed. An Abstract Method is one that is declared without an implementation and needs to be necessarily implemented in the subclass.
if a method is overloaded, does it still inherit the original method from the parent class? What about overriding? Would it also inherit the original plus the overridden one?
if a method is overloaded, does it still inherit the original method
from the parent class?
Yes, you can use both of them.
What about overriding? Would it also inherit the original plus the
overridden one?
Again - yes, but in this case to access the original method you'll have to use a call super.originalMethod().
if a method is overloaded, does it still inherit the original method from the parent class?
yes.
What about overriding?
yes, but the overidden method is not accessible to other classes.
Would it also inherit the original plus the overridden one?
yes.
In case of overloading, the method in the parent class is still available in the child class.
In case of overriding, the method doesn't run the parent's version unless explicitly called using super.method().
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();