Difference between Overriding and Overloading? [duplicate] - java

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 7 years ago.
Just trying to clarify the difference between overloaded & overridden methods... Consider the scenario below;
Say I have a class, let's say 'class Master'. Suppose I have sub class, 'class Apprentice', such that the two classes are related by inheritance.
public class Apprentice extends Master
Hypothetically considering that the master class contains 2 void methods each named attack, one takes a Single string argument, the other a String and an Int argument.
public class Master{
void attack(String bodyPart){
//code for attacking
}
void attack(String bodyPart, int Damage){
//code for specific attack
}
If the Apprentice class has 2 methods named the exact same which takes exactly similar arguments, would the attack Method defined in the Master class be overloaded or overridden?
Wouldn't it be both overridden and overloaded!?

Overriding:
class A {
public void do() { /* ... */ }
}
class B extends A {
#Override
public void do() { /* ... */ } // have an other definition than the one in A
}
Overloading:
class C {
public void do(E e) { /* ... */ }
public void do(E e, F f) { /* ... */ } // same name but different arguments
}
They are two different concepts.

Related

Static / Compile-time Polymorphism [duplicate]

This question already has answers here:
What is the difference between dynamic and static polymorphism in Java?
(14 answers)
Closed 3 years ago.
Why method overloading called as static or compile-time polymorphism
sample in Java.
class StaticPolymorphismSample {
void polymorphicMethod(int a) {
}
void polymorphicMethod(int a, int b) {
}
void polymorphicMethod(String a) {
}
void nonPolymorphicMethod(int a) {
}
void nonPolymorphicMethod1(int a) {
}
}
so my question is.
Why we say that method overloading ( in this case polymorphicMethod methods ) are static polymorphism , but another methods( nonPolymorphicMethod(int a) nonPolymorphicMethod1(int a) ) are not polymorphism.
technically I cannot see different between method with same name and different parameters and method with different,
all answers in here and topics in google is not applicable for my question.
For nonPolymorphicMethod1(int a) the reason this wouldn't be considered polymorphic is because it has a different name from the other nonPolymorphicMethods.
For nonPolymorphicMethod( int a, int b ) and nonPolymorphicMethod( int a ) these aren't considered polymorphic as they don't take the same parameters. Edit This is Wrong See Next Line
The other methods you've shown are polymorphic due to their sharing of a name, but differing parameter types or number of parameters.
A better example of polymorphism in methods would be :
public abstract class ClassA
{
public Object getObject()
{
return new Object();
}
}
public class ClassB extends ClassA
{
#Override
public ClassB getObject()
{
return new ClassB();
}
}
public class ClassC extends ClassA
{
#Override
public ClassC getObject()
{
ClassC example = new ClassC();
example.doStuff();
return example;
}
private void doStuff()
{
// Do Something To Change The Object
}
}

Why does java prefer the parent class in this example? [duplicate]

This question already has answers here:
A superclass method is called instead of the subclass method
(3 answers)
Java inheritance (method overriding and overloading)
(4 answers)
Inheritance and Overloading methods with different argument data types in Java
(2 answers)
Closed 4 years ago.
So in class, we are going over-loading and over-ridding though for some reason my professor could not explain why when we have two classes (a and b; as well as b extends to a) when calling a method it prefers the method in the parent class even though the parameters meet those of the child class. Yet java uses the parent one.
class B extends A {
public int m(int x) {
return 20;
}
}
class A {
public int m(double x) {
return 10;
}
}
public class Tester {
public static void main(String[] args) {
A x = new B();
System.out.println(x.m(5));
}
}
Output: 10

tricky method Overloading in java [duplicate]

This question already has an answer here:
Method overloading/overriding in Java subclass object with superclass reference variable
(1 answer)
Closed 6 years ago.
class ABC {
void doMe(String s) {
System.out.println("String");
}
}
class XYZ extends ABC {
void doMe(Object o) {
System.out.println("Object");
}
}
public class StopStart {
public static void main(String[] args){
ABC o = new XYZ();
o.doMe(null);
}
}
What will happen and why?
string class extends Object. So which doMe() will execute.
Is it a compilation error?
Overloading is resolved in compile time, according to the compile time type of the variable o. Therefore doesn't matter which methods are defined in the XYZ sub-class. Only the methods of ABC are relevant.
Therefore only void doMe(String s) is considered by the compiler, and that's the chosen method.

How interface support multiple inheritance [duplicate]

This question already has answers here:
Multiple inheritance on Java interfaces
(5 answers)
Closed 7 years ago.
public class Test implements X, Y { //X.Y interface are shown below
public void myMethod() {
System.out.println(" Multiple inheritance example using interfaces");
}
public static void main(String[]args) {
Test t=new Test();
t.myMethod();
System.out.println(t.a); //compile time error ambigious field
}
}
Please help me to solve this issue
interface X {
public void myMethod();
int a = 0;
}
interface Y {
int a = 9;
public void myMethod();
}
Any variable defined in an interface is, by definition, public static final, in other words it's just a constant, it's not really a field (since there are no fields in interfaces).
So the compilation error you get points out that the compiler doesn't know witch constant you refer to.
You have 2 options here:
change the name of the constant in one of the interfaces, for example in interface Y declare int b = 9;
inside the main method point to a concrete constant: System.out.println(X.a);
Adding to one of the answer already provided.
If a class implements two interfaces and each interface have method with same signature and same name, then you are effectively defining only one method and they are same. If you have two methods of same name but different return types then there will be a compilation error.
Example ->
public interface A {
int a = 0;
void myMethod();
}
public interface B {
int a = 0;
void myMethod();
}
public class Test implements A, B {
#Override
public void myMethod() {
// My method is defined for both A and B
System.out.println(" Multiple inheritance example using interfaces");
}
public static void main(String[] args) {
Test object = new Test();
((A)(object)).myMethod();
((B)(object)).myMethod();
System.out.println(((A)object).a); //To print constant of A
System.out.println(((B)object).a); //To print constant of B
}
}
//Let's see other example
public interface A {
void myMethod();
}
public interface B {
boolean myMethod(); //changed void to boolean
}
public class Test implements A, B {
#Override
public void myMethod() { //Compilation error here, return type is incompatible
}
}
myMethod implements both myMethod declarations. I believe your problem is that in the two separate interfaces a has different values and is before and after the declaration of myMethod. The order is not important, since myMethod will surely be called after the declaration of a, but the difference of a values might cause some logical discrepancies. Maybe you could implement a getter for it as well, to handle the situation.

How to invoke base-class method from second-level inherited class? [duplicate]

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();
}
}

Categories