1 2 Previous Next 19 Replies Latest reply: Oct 6, 2009 11:50 AM by 796440 RSS

    Java questions help (INHERITANCE)

    807580
      Hi guys, I am having problems with three true/false questions about INHERITANCE. Can you please tell me if I got them right:

      1) A subclass may access private data in the superclass (FALSE)

      2) When a method is overridden, additional exceptions can be added to the throws list. (TRUE)

      3) A public method can only be overridden with another public method. (FALSE)
        • 1. Re: Java questions help (INHERITANCE)
          800456
          Why not write some code and try to compile it?
          • 2. Re: Java questions help (INHERITANCE)
            807580
            Yes, you got it all right. :)

            1) A subclass may access private data in the superclass (FALSE) : Private data is always private for all other classes except for the same class in which it is declared in.

            2) When a method is overridden, additional exceptions can be added to the throws list. (TRUE) : dats is also one of the way to override a method

            3) A public method can only be overridden with another public method. (FALSE): public method can be overridden with any method in the subclass.
            • 3. Re: Java questions help (INHERITANCE)
              800387
              You cannot reduce the visibility of a method when overriding. You cannot add exceptions when overriding. Write out some code and see why.

              - Saish
              • 4. Re: Java questions help (INHERITANCE)
                800387
                shivz wrote:
                Yes, you got it all right. :)

                1) A subclass may access private data in the superclass (FALSE) : Private data is always private for all other classes except for the same class in which it is declared in.

                2) When a method is overridden, additional exceptions can be added to the throws list. (TRUE) : dats is also one of the way to override a method

                3) A public method can only be overridden with another public method. (FALSE): public method can be overridden with any method in the subclass.
                Please do not confuse people with drivel. #1 is ok. #2 is nonsensical. Try #3 for yourself and get back to me on that.

                - Saish
                • 5. Re: Java questions help (INHERITANCE)
                  800456
                  Try compiling this
                  class Superclass
                  {
                      public void poke()
                      {
                          
                      }
                  }
                  
                  class Subclass extends Superclass
                  {
                      public void poke() throws FileNotFoundException
                      {
                          
                      }
                  }
                  
                  and this
                  class Superclass
                  {
                      public void poke()
                      {
                          
                      }
                  }
                  
                  class Subclass extends Superclass
                  {
                      protected void poke() throws 
                      {
                          
                      }
                  }
                  • 6. Re: Java questions help (INHERITANCE)
                    800387
                    andrew_malcolm wrote:
                    Try compiling this
                    class Superclass
                    {
                    public void poke()
                    {
                    
                    }
                    }
                    
                    class Subclass extends Superclass
                    {
                    public void poke() throws FileNotFoundException
                    {
                    
                    }
                    }
                    
                    and this
                    class Superclass
                    {
                    public void poke()
                    {
                    
                    }
                    }
                    
                    class Subclass extends Superclass
                    {
                    protected void poke() throws 
                    {
                    
                    }
                    }
                    He'll probably reply that his javac is broken after that. :^)

                    - Saish
                    • 7. Re: Java questions help (INHERITANCE)
                      PhHein
                      Saish wrote:
                      Please do not confuse people with drivel. #1 is ok. #2 is nonsensical. Try #3 for yourself and get back to me on that.

                      - Saish
                      But, but roseindia says so.
                      • 8. Re: Java questions help (INHERITANCE)
                        800387
                        PhHein wrote:
                        Saish wrote:
                        Please do not confuse people with drivel. #1 is ok. #2 is nonsensical. Try #3 for yourself and get back to me on that.

                        - Saish
                        But, but roseindia says so.
                        Missed that between all the ads and multicolored text examples and strange layouts of boxes on their site. :^)

                        - Saish
                        • 9. Re: Java questions help (INHERITANCE)
                          800456
                          That site is a virus !
                          • 10. Re: Java questions help (INHERITANCE)
                            807580
                            Yes we will not be able to compile these.
                            I didnt said that write incomplete code and that will work fine..
                            And if some one is wrong some where .. you shuld help him make understand instead of making fun of it..
                            • 11. Re: Java questions help (INHERITANCE)
                              PhHein
                              shivz wrote:
                              Yes we will not be able to compile these.
                              I didnt said that write incomplete code and that will work fine..
                              And if some one is wrong some where .. you shuld help him make understand instead of making fun of it..
                              Reread reply #4!
                              • 12. Re: Java questions help (INHERITANCE)
                                800456
                                Nobody is making fun of anyone.
                                The point is, the compiler is a good point of reference for these things, as it implments the Java spec. Also, learning b y active participation rather than passive 'tell me the answer' will help to reinforce the lesson. Programming is a practical occupation, if you can't fire up a computer to check things like this, how are you ever going to be a programmer?
                                • 13. Re: Java questions help (INHERITANCE)
                                  807580
                                  shivz wrote:
                                  And if some one is wrong some where .. you shuld help him make understand instead of making fun of it..
                                  You are not the topicstarter. You were posting answers as if you were knowing where you're talking about. If you don't please refrain from answering as long as you don't understand it yourself. Otherwise it would only confuse the topicstarter more and more.

                                  Oh, this topic actually belongs in 'New to Java' forum. Please use that forum for future questions which are already answered in the basic Sun tutorials.
                                  • 14. Re: Java questions help (INHERITANCE)
                                    807580
                                    okay.. i agree..
                                    but i just had the problem with the comment : "h'll say his javac is broken after that".
                                    everything else is ok to me..
                                    anyways no problem we are here to discuss on java ..
                                    so closing this discussion here.
                                    1 2 Previous Next