Results 1 to 8 of 8
  1. #1
    Jaevko is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default inheritance question

    Getting back to Java/OO, and programming in general. I decided to make a little project (using netbeans) to experiment with classes. I created an animal class, a canine class, and the main class. I immediately ran into an issue:

    Main class:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package oolearning;
    
    /**
     *
     * @author Wildcat
     */
    public class Oolearning {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            animal spot = new canine();
            spot.walk();
            spot.barking();
        }
    }
    animal class:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package oolearning;
    
    /**
     *
     * @author Wildcat
     */
    abstract public class animal {
        
        public void walk() {
            System.out.println("im walking");
        }
        
    }
    canine class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package oolearning;
    
    /**
     *
     * @author Wildcat
     */
    public class canine extends animal {
        public void barking() {
            System.out.println("Ruff Ruff");
        }
    }
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: oolearning.animal.barking
    at oolearning.Oolearning.main(Oolearning.java:20)
    Java Result: 1

    The weird thing is that when I change
    Java Code:
    animal spot = new canine();
    to
    Java Code:
    canine spot = new canine();
    it works fine. Also when I leave it how it is but comment out the barking line in the main method it is fine as well (as if the object were being created as the reference type, an animal, and not the type I intend, a canine). My book I'm studying from (just starting the polymorphism section) says it's okay if "the reference type [is] a superclass of the actual object type". The example it actually gives is
    Java Code:
    Animal myDog = new Dog()
    although thats pretty much just pseudocode, it's not part of a larger thing. Is my book out of date? Or am I doing something wrong?

    Thanks in advance!
    Last edited by Jaevko; 08-31-2011 at 05:32 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You have declared the spot variable to be type animal. The animal class does not have a barking method. Consider the following:
    Java Code:
    class Feline extends Animal {
        public void meow() {
            System.out.println("Meow");
        }
    
        public static void main(String[] args) {
            Animal spot = new Feline();
            spot.barking();
        }
    }
    Obviously that does not work. So it is also not allowed in your situation either. By the way class names should be uppercase as in my code.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You should follow the conventions of your book and start classes with an uppercase letter and methods with a lowercase one. Also methods are best as verbs.

    -----

    If you declare spot to be an animal then the compiler will only let you call animal methods and not those that are dog specific. It does not matter that, at runtime spot is a dog: it's what he was declared to be that matters. If you want to hear spot bark he should be declared to be a canine (as you've found).

    What your book was getting at when it said that "Animal myDog = new Dog();" is OK is that you can declare myDog that way. It's just that you can't have myDog bark.

    (It is possible to have an animal bark so long as that animal really is a dog. This involves a "cast" which basically tells the compiler that what you have declared as an animal is, more specifically, a dog. Casting will be described in your book.)

  4. #4
    Jaevko is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply! So if what you said is correct, my book seems to be wrong. The books says it's okay if "the reference type [is] a superclass of the actual object type". The example it actually gives is

    Java Code:
    Animal myDog = new Dog()
    Where, according to the book, the reference type/superclass is Animal and the actual object type is dog. This conflicts what you said when you said

    You have declared the spot variable to be type animal.
    because if the book is correct then
    Java Code:
    animal spot = new canine();
    declares the spot variable to be type canine, NOT type animal.

    Is my book out of date or am I just being dumb?

    P.S. the book is Head First Java 2nd edition, a book with an excellent reputation from what I've heard, but maybe it's just gotten old...
    Last edited by Jaevko; 08-31-2011 at 05:54 AM.

  5. #5
    Jaevko is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks pbrockway2, I get it now, seems the book isn't wrong but maybe was just a bit misleading until I read more. Thanks for the help guys!!

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What you have to remember is that spot is just a variable. It can reference an Animal object but it IS NOT an object itself. So a variable of the superclass can be assigned a reference to a subclass object. This is perfectly fine and happens all the time

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The example

    Java Code:
    Animal myDog = new Dog();
    is fine as a declaration of a reference to an Animal, which is what your book means by saying it is "OK".

    If your book actually says that this is a declaration of a variable as a reference to Dog then that would be wrong. The declaration all happens on the left hand side of the =. What happens after that (the assignment of a reference to Dog to that variable) is something else again.

    Or if the book gave code that showed a variable thus defined being used to call methods that are proper to Dog instances, that would be wrong.

    But, as it stands, the line is OK as the declaration of a variable as a reference to Animal combined with the assignment of a reference to Dog to that variable.

    -----

    It's tricky at first, but there are two types here (and two types of types?!). The variable has a type (Animal) which will determine what methods can be called, and the value assigned to it has a type (Dog) which will determine exactly what will happen when the method is called (SeaLion instances bark quite differently...).

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    What you have to remember is that spot is just a variable.
    Really?

    db

Similar Threads

  1. Superclass and Inheritance question.
    By Dark in forum New To Java
    Replies: 6
    Last Post: 05-06-2011, 07:45 AM
  2. A question about inheritance / polymorphism
    By gabriels in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 11:53 AM
  3. Question about inheritance
    By hei1233212000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:38 AM
  4. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 05:04 AM
  5. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 10:07 PM

Posting Permissions

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