• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding creating object  RSS feed

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I have one doubt that is what is the meaning of creating object like


B b=new B(); and A a=new B();

Please help me
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A a = new A();

means the an object of type A is created in memory and the reference to that memory location is stored in reference variable 'a';
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also means that the reference "a" is declared and set to type "A".
 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have given difference for that two i need what the use if we create A a=new A() and A a=new B() in which situation we can use those thinks.



Thanking You.
 
Gopi Chella
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do A a = new A(); then you are just creating object for Class A.

However you can do A a = new B(); here polymorphism and Inheritance plays the main role. Here actually you are passing the Sub Class object to the Super class reference like below,



if you do A a = new B(); then you can able to get Super class methods only(ofcourse, includes the methods after overriding but not methods belongs to Class B which is not part of overriding).

In jave if you see the collections then you can understand this, ex List ls = new ArrayList();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!