Is this method overloading, overriding or none? [duplicate] - java

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 7 years ago.
Difference between method overloading and overriding in java? does not give the correct answer. Below is java code.
Parent class
public class Parent {
void display() {
// some code
}
}
Child class
public class child extends Parent
void display(int a) {
// some code
}
}
Question: Is this Method overloading, overriding or none?

That's overloading (in child), because JLS 8.4.9:
If two methods of a class (whether both declared in the same class, or
both inherited by a class, or one declared and one inherited) have the
same name but signatures that are not override-equivalent, then the
method name is said to be overloaded.

This is Overloading
Method Overloading - method in Same Class or different class
Method Overriding - method both in Parent Child class
Here method has different signature in both Parent and Child class

Related

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

How is this called overloading - Java [duplicate]

This question already has answers here:
Java overloading and overriding
(9 answers)
Closed 5 years ago.
I have this class BindingSample with a method that takes no parameter
public class BindingSample {
public void printMsg(){
System.out.println("Binding Sample no parameter");
}
}
And another class that extends the BindingSample and uses the same method signature but adds a parameter to it
public class application extends BindingSample {
public void printMsg(int i){
System.out.println("Changed my value " + i);
}
public static void main(String[] args) {
application app = new application();
app.printMsg(5);
}
}
The output is Changed my value 5
Why did it work even if the parameters are different? And why is it called overloading? I don't think it's overriding because to override a method, the method signature and its parameter should be the same.
Why did it work even if the parameters are different?
You application class has a printMsg method that takes a single int argument. Therefore app.printMsg(5) works.
Note that making the following change will cause your code not to pass compilation:
BindingSample app = new application();
app.printMsg(5);
since now the compiler can't find a printMsg method in the BindingSample class that takes an int argument.
And why is it called overloading? I don't think it's overriding because to override a method, the method signature and its parameter should be the same
Overriding and overloading are two different concepts.
Method overloading occurs when multiple methods share the same name but have different number of arguments or different types of arguments.
Your application class has two printMsg methods (one inherited from its super class) with a different number of arguments. Hence this is method overloading.
Overloading means just the names of the methods should be same. Everything else can be different (e.g. arguments, return types, throwable exceptions etc). As both the methods in the example have same name, it's called Overloading.
The reason why it works is, Overloading is compile time phenomena, compiler just checks for accessibility of the method to the object that's calling it. As you are instantiating application class and calling printMsg method with int argument. It works fine because of the method being present in the class (or being accessible to that object).

Why instance method cannot override the static method [duplicate]

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 5 years ago.
class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
Compiler gives error at line 3 saying that
This instance method cannot override the static method from Abc
Why can static methods not be overridden by a instance method?
Simple: because the language specification says so.
That is one of the downsides of static methods: there is no polymorphism for them! Conceptually, they are not meant to be overridden. That is all there is to this.
To be precise: the JLS says differentiates between static and non-static method and states:
An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.

Difference between super and ((parent)this)? [duplicate]

This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Java cast to superclass and call overload method
(1 answer)
Closed 5 years ago.
Super keyword related doubt.
class Parent{
int x=40;
void show()
{
System.out.println("Parent");
}
}
class Child extends Parent
{ int x=20;
void show() //method overriding has been done
{
System.out.println(super.x); // prints parent data member
System.out.println(((Parent)this).x); /*same output as previous statement which means super is similar to (Parent)this*/
System.out.println("child");
super.show(); // invokes parent show() method
((Parent)this).show(); //Doesnt invoke parent show() method.Why?
}
public static void main(String s[])
{
Child c1=new Child(); //Child class object
c1.show();
}}
So, System.out.println(super.x) and System.out.println(((Parent)this).x) prints the same value.So if super.show() calls parent class show() method then why is ((Parent)this).show(); unable to call parent show()? Please tell appropriate explaination for this.
Constructor Chaining
in Java keyword this represent current object and when one class extends another then its super class reference variable may hold child class reference variable that's is in your code is ((Parent)this).x ,
while super keyword is used to call directly super class constructor and its variables.
at the same time when super class variables holds child class object and when we use super it refers same object .
How to call one constructor from another constructor in Java or What
is Constructor Chaining in Java is one of the tricky questions in Java
interviews. Well, you can use this keyword to call one constructor
from another constructor of the same class if you want to call a
constructor from based class or super class then you can use super
keyword. Calling one constructor from other is called Constructor
chaining in Java. Constructors can call each other automatically or
explicitly using this() and super() keywords. this() denotes a
no-argument constructor of the same class and super() denotes a no
argument or default constructor of parent class. Also having multiple
constructors in the same class is known as constructor overloading in
Java.
Read more: http://www.java67.com/2012/12/how-constructor-chaining-works-in-java.html#ixzz4bJ5C069o
Calling ((Parent) this).show(); on what is really a Child object causes Child.show() to be called. Whether you are doing it as myObject.show() or through this makes no difference for which version of the method gets called — it’s always the method determined by the object’s runtime type, in this case Child.
So you have a recursive call, leading to infinite recursion.
super.show() on the other hand calls the method in the super class, in this case Parent.

Override vs Shadowing in Java [duplicate]

This question already has answers here:
Overriding vs Hiding Java - Confused
(17 answers)
Closed 9 years ago.
What is the difference between overriding and shadowing in particular to this statement
“A static method cannot be overriden in a subclass, only shadowed"
If you were truly overriding a method, you could call super() at some point in it to invoke the superclass implementation. But since static methods belong to a class, not an instance, you can only "shadow" them by providing a method with the same signature. Static members of any kind cannot be inherited, since they must be accessed via the class.
Overrideing... This term refer to polymorphic behavior for e.g
class A {
method(){...} // method in A
}
class B extends A {
#Override
method(){...} // method in B with diff impl.
}
When you try to invoke the method from class B you get overriden behvaior ..e.g
A myA = new B();
myB.method(); // this will print the content of from overriden method as its polymorphic behavior
but suppose you have declared the method with static modifier than by trying the same code
A myA = new B();
myA.method(); // this will print the content of the method from the class A
this is beacuse static methods cant be overriden ... the method in class B just shadow the method from class A...

Categories