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

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

Related

Using of methods instead of constructor [duplicate]

This question already has answers here:
Purpose of a constructor in Java?
(12 answers)
Closed 2 years ago.
When we can use methods instead of constructor for any operation then what is the use of constructor in java or c++.
//Program of Division using constructor:-
class Hey {
Hey() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main ( String[] args ) {
Hey ob = new Hey();
}
}
//Program of division using method:-
class Hey {
public void disp() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main( String[] args ) {
Hey ob = new Hey();
ob.disp();
}
}
As, we can see that both will have same output. So, now I am bit confuse that when to use constructor.
Constructor is used to initialize objects in java. Even if you don't provide constructor in your code, java compiler will automatically add a default constructor.
Whereas Methods are used to exhibits functionalities to object. You will have to invoke methods explicitly in your code.
In the example you shared, you are creating object of Hey class Hey ob=new Hey() in order to call its method disp. So if you want to define object in your class, you will use constructors, and if you want to write some functionality of object, you can use Methods.

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
}
}

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.

Difference between Overriding and Overloading? [duplicate]

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.

Default methods in interface - modifier order [duplicate]

This question already has answers here:
Java modifiers syntax and format
(5 answers)
Closed 8 years ago.
I wonder what order is better in following examples:
Example 1
public interface Foo {
public default int bar() {
return 1;
}
}
Example 2
public interface Foo {
default public int bar() {
return 1;
}
}
Considering public is implicit = optional, your problem is virtually non-existent
public interface Foo {
default int bar() {
return 1;
}
}
Of course you can add it, but it doesn't have any effect.

Categories