This question already has answers here:
Vararg methods Override/Overload confusion
(3 answers)
Closed 4 months ago.
Could someone explain why while upcasting when we have varargs in parent class, the method of parent class executes instead-of child's one?
public class Test {
public static void main(String[] args) {
A a = new B();
a.foo("123");
}
}
class A {
public void foo(String... s) {
System.out.println("A");
}
}
class B extends A {
public void foo(String s) {
System.out.println("B");
}
}
Could someone explain how it works?
The type of a variable defines the interface you can use to interact with the object it's referring to. In this case, you are referring to a B instance through a variable of type A. Because A has only the varargs version of foo(), only this one is available for the compiler to choose.
Related
This question already has answers here:
What is the difference between a variable, object, and reference? [duplicate]
(5 answers)
why can't I call a subclass method using a reference of a parent type that refers to an instance of a sub-type?
(6 answers)
Closed 3 years ago.
I was wondering if it's possible to define a method within the implementation of an interface class and call that method from another class?
for example:
public interface MyInterface
{
void method1();
}
public class MyClass1 implements MyInterface
{
public void method1()
{
System.out.println("This is already defined in MyInterface")
}
public void method2()
{
System.out.println("This is not already defined in MyInterface")
}
}
public class MyClass2
{
public static void main(String[] args) {
{
MyInterface interface = new MyClass1()
interface.method2
}
}
This question already has answers here:
How is an overloaded method chosen when a parameter is the literal null value?
(8 answers)
Closed 3 years ago.
public class Main {
static void method(A a){
System.out.print("one");
}
static void method(B b){
System.out.print("two");
}
static void method(Object obj){
System.out.print("three");
}
public static void main(String[] args) {
C c = new C();
method(c);
}
}
class A {}
class B extends A{}
class C extends B{}
As you see the title, i think it displays "three" but true answer is "two". Anyone can explain me? Thankss!
Overloading will resolve to the most specific type that applies to the argument. All of A, B and Object could apply to C, but the most specific of those is B. So method(B) will be called.
If C did not extend A or B, then method(Object) would be called.
This question already has answers here:
What is polymorphism, what is it for, and how is it used?
(29 answers)
Polymorphism vs Overriding vs Overloading
(21 answers)
Try to describe polymorphism as easy as you can [closed]
(24 answers)
Polymorphism - Define In Just Two Sentences [closed]
(31 answers)
Closed 5 years ago.
I have these two classes and I want to know why the output is A, B, and B.xxx. Can someone explain why it is not A.xxx when it is casted?
Here are my two classes and the main method:
public abstract class A {
public A() {
System.out.println("A");
}
public void xxx() {
System.out.println("A.xxx");
}
abstract void yyy();
}
public class B extends A {
public B() {
System.out.println("B");
}
public void xxx() {
System.out.println("B.xxx");
}
public void yyy() {
System.out.println("B.yyy");
}
}
public class ClassRunner {
public static void main(String[] args) {
B b2 = new B();
((A)b2) .xxx();
}
}
So when you have inheritance...a variable initialization of the form
A my_A = new B()
where B extends A...is called disguising. You are disguising an instance of B as an object of A. And the rule of thumb is that the class on the left side of the equals sign (A) defines which methods you are allowed to call on your object. Since class A has the ".xxx()" method, we are allowed to use it. On the other hand, the class on the right hand side of the equals sign defines the actual version of the .xxx() method that we use. Since class B is on the right side, we use B's version of .xxx(). If B does not have a .xxx(), then it defaults to using A's version of it.
This question already has answers here:
How to call an non overide method of child class using parent object reference
(3 answers)
Closed 8 years ago.
*
class Abc {
void m1() {
}
}
class Bat extends Abc {
void m2() {
}
}
class Cat extends Bat {
void m3() {
}
}
class D extends Cat {
void m4() {
}
}
public class Check {
public static void main(String[] args) {
Abc a = new Bat();
a.m1();
((Bat) a).m2();
}
}
Why are we casting the created object to Bat in order to access m2() when that method is of Bat class and we have created its object also??
the m2 method is not defined as a method on the assigned type Bat, which will therefore result in compile time issue; this could be solved by casting;
Although a new instance of Cat was created, its reference is by a which is declared to be of the type Abc. Therefore, any references to a makes the new Cat be handled as an Abc.
This question already has answers here:
Are static methods inherited in Java?
(15 answers)
Closed 9 years ago.
In Java, "this" refers to the current object. I assumed that "this" is the same type as the current object, but consider this example:
class A {
static void f() {
System.out.println("A.f");
}
void g() {
this.f();
}
}
class B extends A {
static void f() {
System.out.println("B.f");
}
}
public class C {
public static void main(String[] args) {
B test = new B();
h(test);
}
static void h(B x) {
x.g();
}
}
The result is:
A.f.
Which I don't understand, because when x.g() is called, x is of type B. In the x.g() call, g is looked up in B, then in A (because B subclasses A). g then calls f, an instance method of both A and B, meaning that the version of f called depends on the type of the implicit THIS parameter. I would assume that B.f() would be called since X is of type B, but this is not so.
What type does THIS take on, exactly?
static methods are not inherited. When you call
static void h(B x) {
x.g();
}
You are calling g() declared in class A which calls
static void f() {
System.out.println("A.f");
}
Methods are resolved on the static type of the reference they are called on. For instance methods, polymorphism and late-binding do their trick to execute the actual method. However, since late binding doesn't apply to static methods, you are calling A.f().
You can call static methods on instance references and they are resolved on their declared type. This is not recommended.