• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Confused about overloading and overriding

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused about the questions related to overloading and overriding. There r a lot of questions that ask at what time the error will occue : compile time or runtime.These questions r generally based on a subclass object being assigned to a superclass reference.
Please help me regarding these questions.
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic