Why is casting required in Inheritance [duplicate] - java

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.

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.

the return value of an instance method called by superclass reference are incompatible with the declared return type [duplicate]

This question already has answers here:
Java method overriding covariance inquiry
(3 answers)
Closed 1 year ago.
public class Confusion {
public static void main(String[] args) {
F f = new S();
//System.out.println(f.test().a); //from papa string a
//f.test().pb();//from son
AnotherSon as = f.test();//Incompatible types. Found: 'com.solution.day10.AnotherFather', required: 'com.solution.day10.AnotherSon'
}
}
class F {
AnotherFather test() {
System.out.println("from father");
return new AnotherFather();
}
}
class S extends F {
#Override
AnotherSon test() {
return new AnotherSon() ;
}
}
class AnotherFather{
String a ="from papa string a";
public void pb(){
System.out.println("from papa");
}
}
class AnotherSon extends AnotherFather {
String a = "from son string a";
public void pb(){
System.out.println("from son");
}
}
As you can see, the test method in class S overrides its superclass' method with changing the return type from AnotherFather to AnotherSon which is subclass as defined.
var f is declared as type of F but assigned to an instance of Son. f.test() should run the test() code in class S,as indeed it is ,if you debug the program.
Since test() of class S is executed, it's natural thing to use type of AnotherSon to get the result. But I get the Incompatible types. Why is that?
In class S you are returning the child class and in class F you are returning the base class. So when you call f.test() it only knows about AnotherFather. The return type AnotherSon cannot be resolved from AnotherFather. It would have worked the other way round.

Method not found in derived class inherited from abstract class [duplicate]

This question already has answers here:
Superclass reference not able to call subclass method in Java
(2 answers)
Closed 2 years ago.
abstract class superclass {
public abstract void method();
}
class subclass extends superclass {
public void method() {
//do something
}
public void newMethod() {
//do something
}
}
public class mainclass {
public static void main(String[]args) {
superclass abc = new subclass();
abc.method();
abc.newMethod(); //cannot find symbol error
}
}
In the above example, can new methods be not written in the derived class of an abstract class? If I do that, it raises an error.
When extending a Superclass you can in fact add more methods. Hoevery in this case you assign your new subclass() to a variable of the type superclass which means that you will only have access to the methods wich are a part of that type. In this case that's only method(). If you want to use both methods you should write instead:
subclass abc = new subclass();
or cast it on demand:
((subclass) abc).newMethod();

Calling a method defined within an interface implementation from another class in java [duplicate]

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

Type of "this" keyword? [duplicate]

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.

Categories

Resources