-3

I am stuck at this portion.

class1 cls = new class2(); //class1 reference and class2 object

I know it is method overriding, But somehow not getting a clear vision what is actually going behind the scenes. I need a clear view as I can implement this where I need it. Any answer with example makes more clear concept. Thanks

Sunny
  • 73
  • 1
  • 1
  • 9
  • 1
    http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html – khelwood Feb 18 '15 at 13:06
  • 2
    You should read a good beginners book on Java programming before going near an editor and compiler. –  Feb 18 '15 at 13:08
  • Double docs! Also the names you have used are misleading. Something like `Class1 myObject = new Class2();` might make it easier to understand – rbennett485 Feb 18 '15 at 13:09
  • Yeah, method declaration was wrong :) , ok now – Sunny Feb 19 '15 at 08:17
  • @Sunny There isn't anything in your post that coul be called a "method declaration". Seriously, you shouldn't try doing anything specific before learning the basics. – gvlasov Feb 25 '15 at 10:09

3 Answers3

3

Well, its not method overriding, its like the following case,

  1. Horse is an Animal. In Java you will say that Horse is a sub-class of Animal.

  2. Now, lets say there is a horse whose name is "lightening". In some java sense you can write Horse horse1 = new Horse( "loghtening" );

  3. But again... since every horse is an Animal.. so you can also write, Animal animal1 = new Horse( "loghtening" );

  4. But then... what is the difference... ? Well... the difference is that when you are referring to an animal, you should talk about things which apply to all animals... not just horses. In any conversation... If you say something "horse-specific" about any animal... people will get confused. For example you can not just say "I had a ride on back of my pet animal Lightenning"... well... Others don't know Lightening is horse and you can not ride on all animals. Similarly, In Java even if Horse class has a method rideOnMyBack you can not call animal1.rideOnMyBack()... because in your conversation with Java... animal1 is referred as an Animal

sarveshseri
  • 13,738
  • 28
  • 47
1

The line of code in your question is often referred to as "programming to an interface". It is not "method overriding".

Search for this on, e.g., this site. E.g. look at. What does it mean to "program to an interface"?

Community
  • 1
  • 1
Christian Fries
  • 16,175
  • 10
  • 56
  • 67
0

Your question's code snippet should be Class1 class1 = new class2(); not class class1 = new class2(); as you can't assign any object to reference of class.

And This is not method overriding.

Method Overriding is something where you write a method in your class with the same name, same parameters and same return type as one of the methods in its super class.

In your case, it's called Type Casting or Auto boxing/ unboxing. Suppose in your case Class2 extends Class1 then when you will instantiate Class2 actually in memory there will be three objects created in the memory as per shown in the image below -

enter image description here

Now when you'll do Class1 class1 = new class2(); actually reference of Class1 itself will be assigned. But according to the code you'll have a feeling of assigning object of Class2 in Class1 which is impossible in java.

As a thumb rule, you can assign to a reference only the object of that class or null, nothing else.

Now for accessing methods and variables -

  • Priorities will always be given to methods of subclass. So if you have 2 methods with same name, same parameters then method of sub class will be invoked. This is called method overriding.
  • Instance variables will always be accessed of the same class.
  • "Priorities will always be given to methods of subclass. So if you have 2 methods with same name, same parameters then method of sub class will be invoked. This is called method overriding" --------- This is a lot helpful buddy, absolutely worthy...though I made a mistake in the question :D but it,s ok now – Sunny Feb 19 '15 at 08:12
  • Good to know that u got what you needed. you can upvote and accept the answer then. :) – Rahul Chaubey Feb 19 '15 at 09:04