Reputation: 1003
I can't get a clear idea about inheritance in Java. For instance, I have:
class A
{
...
public void MethodA();
}
Here is everything is clear. Then I want a (sub)class:
class B extends A
{
...
public void MethodB();
}
Then I create an instance of A
. Then I want to get an instance of B
having all the stuff from A
. I want to make something like aA = new B();
but get an error.
Using this thing is not really comfortable:
A aA = new A(...);
B aB = new B(null,null,...);
aB = aA
How should I make it correctly and get a clear idea about inheritance in Java?
UPD: So how can i get an instance of B having all the stuff of A?
Upvotes: 1
Views: 132
Reputation: 2232
You need to understand the IS-A relationship. Let's discuss an example of Employee and Manager relationship.
class Employee{
public Employee(){
System.out.println("Employee obj");
}
}
class Manager extends Employee{
public Manager(){
System.out.println("Manager obj");
}
}
By extending Employee we are declaring Manager as a specialization of Employee. Therefore, every Manager is an Employee but every Employee is not Manager.
A reference of type Employee can refer to the objects who satisfy the IS-A relationship. Therefore, these are perfectly legal to write:
Employee emp1 = new Employee();
Employee emp2 = new Manager();
However, if we try to write
Manager man1 = new Employee(); //Wrong: Compilation error.
This violates the IS-A relationship. As a result of this we get compile time error. In order to avoid this kind of compilation error we need to add a cast to type Manager. But, casting to type Manager only avoids compilation issues. You will get a java.lang.ClassCastException as a result of of this kind of casting.
Manager man1 = (Manager) new Employee(); //Wrong: avoids compilation error but will generate a ClassCastException at runtime.
I suggest you to go through the Java Language Specification: Section 5.5.1 to get a full idea of casting a reference type.
Upvotes: 1
Reputation: 500367
It sounds like you want an instance of B
in the first place. Once you have such an instance, you can use it whenever either A
or B
is required.
Put another way, every instance of B
is automatically also an instance of A
.
This is called the Liskov Substitution Principle.
Upvotes: 4
Reputation: 3489
In your example, A - superclass, B - subclass.
A subclass possesses all the attributes and operations of its superclass (because a subclass inherited all attributes and operations from its superclass). This means that a subclass object can do whatever its superclass can do. As a result, we can substitute a subclass instance when a superclass instance is expected, and everything shall work fine. This is called substitutability.
Upvotes: 1
Reputation: 170745
Every instance of B
has all of stuff from A
, because it is an A
. But it seems like you actually want to create an instance of B
from a given instance of A
. This isn't possible, unless you specifically write your class so as to allow it:
public class B extends A {
public B(A original) { ... }
}
and later
A a = new A();
B b = new B(a);
Upvotes: 0