Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-18-2008, 11:17 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
how to get the child class in inheritance?
hi,
if I have a store for renting videos .. and I have Video class (parent) and two (child) classes DVD and Cassete which they are extend the video class.

And the user wants to rent a DVD which is the child, how do I do that? can I write just getDVD() as simple as that or maybe I need to use a keyword like "getinstance of DVD " or something.

class Video have attributes like title, date of production and director.
I'm not good in inheritance help please.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-18-2008, 11:56 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
Polymorphism
Hello java_fun2007

If you create an instance of DVD or of Cassete, it is also an instance of Video, since DVD and Cassete are extended from Video and contains all the inheritable attributes and methods of Video. That means that you can have a variable, media, that can contain a DVD or Cassete. So lets a media variable with a DVD in it. The new keyword use used to create instances of a class.
Code:
Video media = new DVD();
Now, we know that media is an Video but we might want to know if it is a DVD or a Cassete. For that we use the instanceof keyword for a boolean test.
Code:
if (media instanceof DVD) System.out.println("I'm a DVD"); if (media instanceof Cassete) System.out.println("I'm a Cassete");
I use this stuff regularly, so I hope this helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-19-2008, 12:14 AM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thank you Tim for making this clear to me!

so, to get the title of a DVD I write like this?

Code:
if (media instanceof DVD) System.out.println(media.getTitle());
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-19-2008, 12:43 AM
Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 90
wsaryada is on a distinguished road
If the getTitle() method is defined in the Video class then you don't have to check whether the media is an instance of DVD, because when a Video object refered to an instance of DVD the getTitle() will return the DVD title.
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-19-2008, 12:34 PM
Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 18
jimm1 has a little shameless behaviour in the past
yeah. Rather just have

System.out.println(media.getTitle());

If the title field is defined in the Video super class, then the children inherit it.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-19-2008, 03:47 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
Casting
Hello java_fun2007.

I forgot to tell you about casting. Lets say that you have defined some method in the DVD class that is not inherited or overridden from its super class, but you need to use it. For this you can use casting. When you cast you must use brackets and class name of the class you need. For example:
Code:
Vedio media = new DVD(); if (media instanceof DVD){ // now we know it's an DVD and we can cast DVD dvd = (DVD) media; // now you can use dvd with all the methods and attributes of a real DVD instance. }
I hope this helped. This is pretty important stuff.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-20-2008, 12:23 AM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
thank you so much for your help .

I guess I understand now! If I have another question about inheritance I will ask here again.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance and aggregation java_fun2007 New To Java 3 12-13-2007 02:36 PM
how to iterate a child element in XSD dynamically navya XML 0 12-13-2007 10:56 AM
Delegation vs inheritance javaplus Advanced Java 1 12-07-2007 10:07 PM
Inheritance mew New To Java 1 12-07-2007 07:08 PM
Inheritance in GUI Marty SWT / JFace 2 05-11-2007 01:54 AM


All times are GMT +3. The time now is 04:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org