• Post Reply Bookmark Topic Watch Topic
  • New Topic

please see comments in my program  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:
Base b1=new Obas();//how tis statment works


An Obas IS-A Base, so the assignment is allowed.

bairava surya wrote:
b1.brt();//why tis shows error


At compile time, the compiler doesn't know the actual type of the instance. It only knows the type of the reference, and the reference is of a type that doesn't have such a method.

To call the method, since you know the type, you will have to override the compiler by casting prior to the call.
Henry
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it means that by using b1 reference we can cal base class methods and overriden methods !!!i
 
Deepankar Narang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Base b1=new Obas();//how tis statment works


This works because Obas is a child of Base. Parent reference can always hold child object. Its just that it only knows about parent functionality. New additions by child are not known by the Parent.

Base b1 means you are creating a container which would hold reference to any object which is of type Base. So b1 can hold reference to Base object or to any object which inherits Base. And this inheritance could be at any level i.e. Obas may further have a child
class Gbas extends Obas{}
Base b1 = new Gbas(); //This is valid too


b1.brt();//why tis shows error


b1 is a reference of type Base. Base only knows the behaviour declared within the base class. So any function not known to Base would throw an error.

What makes it further interesting is if child class overrides a method, this overridden method would be called (Dynamic Binding). If a method is not overridden, base class method would be called.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to read our Campfire Story on "How My Dog Learned Polymorphism".

It's well worth your time.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Base b1=new Obas(); no usage of this statment !!!with help of this we can call only overridden methods and base methods !!!is it correct
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:Base b1=new Obas(); no usage of this statment !!!with help of this we can call only overridden methods and base methods !!!is it correct

bairava,

Your comments suggest that you're frustrated with what you're reading or learning, but we're not mindreaders. We can't understand WHAT you're frustrated with unless you tell us.

So please, quit with the "!!!"s and TellTheDetails (←click).

Winston
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alot for your suppor.doubts got cleared.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am preparing for oca java se7.suggest me some top websites for mock exams.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:Am preparing for oca java se7.suggest me some top websites for mock exams.



http://www.coderanch.com/how-to/java/ScjpMockTests
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code titles which tell us what the question is about.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error msg showing while i prompt to write the sjcp exam !provide me solution!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:...!provide me solution!

No. Please read the HowToAskQuestionsOnJavaRanch (←click) page, bairava.

Winston
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:Base b1=new Obas(); no usage of this statment !!!with help of this we can call only overridden methods and base methods !!!is it correct


don't try to mug up, try to undertsand.
BTW you've got your answers already.

Thanks,
Aakash
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!