Results 1 to 8 of 8
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default 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.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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.
    Java Code:
    Video media = [COLOR="Teal"]new [/COLOR]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.
    Java Code:
    if (media [COLOR="RoyalBlue"]instanceof [/COLOR]DVD)
        System.out.println("I'm a DVD");
    if (media [COLOR="RoyalBlue"]instanceof [/COLOR]Cassete)
        System.out.println("I'm a Cassete");
    I use this stuff regularly, so I hope this helped you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    Thank you Tim for making this clear to me!

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

    Java Code:
    if (media instanceof DVD)
    System.out.println(media.getTitle());

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    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.

  5. #5
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    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.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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:
    Java Code:
    Vedio media = new DVD();
    if (media instanceof DVD){
        // now we know it's an DVD and we can cast
        DVD dvd = [COLOR="RoyalBlue"](DVD)[/COLOR] 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. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    thank you so much for your help :D.

    I guess I understand now! If I have another question about inheritance I will ask here again.;)

  8. #8
    Pandiyan is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    hi,
    I have one (parent)class and many (child)classes.
    i need child class's object in parent.
    please reply me.

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2008, 10:18 AM
  2. inheritance and aggregation
    By java_fun2007 in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 01:36 PM
  3. Delegation vs inheritance
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2007, 09:07 PM
  4. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •