In this program why is the parent class allowed to point to
In this program why is the parent class allowed to point to the object of the child class. What effect does this have on the parent class object?
public class SampleClass {
public static void main(String[] args) {
AnotherSampleClass asc = new AnotherSampleClass();
SampleClass sc = new SampleClass();
sc = asc;
}
}
class AnotherSampleClass extends SampleClass {
}
Solution
Answer:
when the child class object is assigned to base class object, Object slicing happens. So the additional attributes of a child class object are sliced off to form the base class object.
Normally child class has all the functionalities of base class plus some extra features or methods. But if we assign this object to base class, these extra features and methods are sliced / can\'t be accessed.
