This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method in parent class.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method in parent class...." Watch "Method in parent class...." New topic
Author

Method in parent class....

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142


Can anybody please explain how the above code works?

If getDetails() is not there in the class Employee then what happens?


thanks
Jyo
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Please put your code between code tags.

Once you extend a class, the Subclass inherits all members of the parent class which are marked as public, protected or, if the Subclass is in the same package, default.

when you assign the instance of a Subclass to the Super Type, and invoke a method on it that is overwritten in the subclass, it is automatically casted to the subclass type and hence the overwritten form runs.

If you do not overwrite a method, you work with the one you inherit from the super class.


JDBCSupport - An easy to use, light-weight JDBC framework -
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Sebastian Janisch wrote:Please put your code between code tags.


Sorry for your inconvinience, I had not put my code into the code tags, as was not knowing .

Sebastian Janisch wrote:Once you extend a class, the Subclass inherits all members of the parent class which are marked as public, protected or, if the Subclass is in the same package, default.

when you assign the instance of a Subclass to the Super Type, and invoke a method on it that is overwritten in the subclass, it is automatically casted to the subclass type and hence the overwritten form runs.

If you do not overwrite a method, you work with the one you inherit from the super class.


Now I am very clear with the concept.

Thanku very much........ .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Jyoti Vaskar wrote:If getDetails() is not there in the class Employee then what happens?

Why don't you just try that out and see for yourself what happens?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Jesper Young wrote:
Jyoti Vaskar wrote:If getDetails() is not there in the class Employee then what happens?

Why don't you just try that out and see for yourself what happens?


I saw by doing the practicle.

I got the error........ but I was anable to understand the error thrown
as was not knowing the actual processing of the code.

But now after explanation I understood.

Hence asked the above question .
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Sebastian Janisch wrote:when you assign the instance of a Subclass to the Super Type, and invoke a method on it that is overwritten in the subclass, it is automatically casted to the subclass type and hence the overwritten form runs.

If you do not overwrite a method, you work with the one you inherit from the super class.


Is the above statement true for only methods.

Its not working same as for the variable.




it displays "0" ie value of parent classe's variable.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Yes, it is only true for instance methods. We have an FAQ (No 16) about that. Only instance methods can be overridden.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
I went through the above topic faq(No 16)its very cleare about the instance methods .

I am here asking about the variables.

It doesnt work same for variables as for members?



it displays "0" ie value of parent classe's variable. For even the instance variable.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Methods only. But not all methods, only instance methods. So overriding doesn't apply to fields. Find a copy of Java Puzzlers (Bloch and Gafter) and look at puzzles nos 66 (p 157), 67, 68, and 72, and the naming glossary on page 180.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Campbell Ritchie wrote:Methods only. But not all methods, only instance methods. So overriding doesn't apply to fields. Find a copy of Java Puzzlers (Bloch and Gafter) and look at puzzles nos 66 (p 157), 67, 68, and 72, and the naming glossary on page 180.


Thank you .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method in parent class....
 
Similar Threads
Derived Class & Method Calling
can you explain me classCastException ?
Polymorphic Arguments
create objects of my classes dynamically and put them into linkedlist
Packages??