hi Gaurav
First of all separate the two problems -
1. Overloading and Overriding
2. Inheritance - subclass / superclass objects being assigned to each other
1. Overloading pertains to methods in same class or subclasses / overloading of constructors in same class
Overriding necessarily refers to subclass ( subclass overriding methods of superclass) - PLS ASK SPECIFIC QUERIES AS THE TOPIC IS TOO VAST.
2. Reg - Inheritance - I have tried to give some explanation thru the foll code - TRY TO
TEST THE VARIOUS OPTIONS ONE AT A TIME( COMMENT THE REST NOT RELEVANT ) - Explanation provided
Similarly try out with classes implementing interfaces.
code :
class B extends A
{
public static void main(
String[] args)
{
A a = new A();
B b = new B();
B b1 = new B();
B b2 = new B();
B b3 = new B();
C c = new C();
// b = a; //superclass object cannot be passed into subclass - incompatible types - COMPILER ERROR
//b = (B)a; // superclass object can be passed into subclass WITH CASTING - just fooling the compilerb- Runtime error ERROR - as Class type
// determined at runtime
a = b1; // subclass object can be passed inyo superclass object - so it is reference type superclass and class type subclass
//b1=a; // now although ' a ' is of subclass type but since it has ref type of superclass - it gives compiler erroe
b2 = (B) a; // now it will compile as well as run because you have changed both the Class type and the Reference type
// to subclass
// b3 = c; // although both are subclasses of A - COMPILER ERROR - as changes possible at subclass level
b3 = (B) c; // as class types are different - just changing reference type with casting still gives COMPILER ERROR
System.out.println("Hello World!");
}
}
class C extends A
{
}
class A
{
};