Casting objects and inheritance [duplicate] - java

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.

Related

Overloading & upcasting Java [duplicate]

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.

Is it not possible to invoke superclass method? [duplicate]

This question already has answers here:
Java How to call method of grand parents? [duplicate]
(3 answers)
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 3 months ago.
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in class A invoke the test()method defined in class C?
Answer is "It is not possible to invoke test()".
class C {
public static void test(){
System.out.println("IMESH ISURANGA");
}
}
class B extends C {
}
class A extends B {
//how prove it
}
class Demo {
public static void main(String[] args) {
//----
}
}
How to prove it?
The answer is "It is not possible to invoke test()". Because if it's possible, it would break encapsulation.
We can invoke the test() method in class B using "super." keyword. This happens under the assumption that we are aware that we are violating the encapsulation on our own class. But we are not allowed to access the class above the parent class using "super." keywords because we don't know what rules the superclass is enforcing. So, we can't bypass an implementation.

Why is overloaded method selected with direct superclass parameter rather than Object? [duplicate]

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.

Polymorphism Concept: Mentioned example is of Method Overloading or Overriding? [duplicate]

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Java overloading and overriding
(10 answers)
Java overloading and overriding
(9 answers)
Closed 4 years ago.
To Which concept below example belongs to: Method Overloading or Method Overriding???
package com.java.inheritance;
class A {
public void display(Integer i)
{
System.out.println("in display of A"+i);
}
}
class B extends A
{
public void display(String i)
{
System.out.println("in display of B class"+i);
}
}
/*public class InheritanceDemo{
public static void main(String args[])
{
A a=new B();
a.display("suchi");
}
}*/
Please suggest with explaination.
This is an example of overloading, as you're applying the same signature except the number/kind of parameter(s).
If it had the same exact signature, it would be overriding.

Accessing protected members [duplicate]

This question already has answers here:
Protected member access from different packages in java - a curiosity [duplicate]
(9 answers)
Closed 7 years ago.
I am very confused with this . can any one clear me why it was not allowing the code that i have placed n comments
package pack1;
public class A {
protected void m1() {
System.out.println("This is very imp point");
}
package pack2;
import pack1.A;
class B extends A {
public static void main(String arg[]) {
// A a1 = new A();
//a1.m1();
B b1 = new B();
b1.m1();
//A a2 = new B();
//a2.m1(); }
}
}
Method m1 is protected, so it's accessible across packages to child classes.
Therefore, instances of B will be able to invoke or #Override m1.
Not so main static methods, even if pertaining to class B: the scope is different.
You can either make m1 public in A, or invoke it within your instance of B (e.g. in the constructor, etc.).
You can also override A's m1 in B and give it less access restrictions, thus making it public in this instance: then you could access it on an instance of B from your main method as you're trying to do.
You can access the protected members declared in A within B, but only for instances of B or subclasses of B. Check out this answer

Categories

Resources