• Post Reply Bookmark Topic Watch Topic
  • New Topic

extended class created objects  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone

Could someone please assist me with a better understanding of why an extended class creates an object when a object is created from the parent class as seen in the question below:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from, to avoid copyright problems.
  • 1: The args array.
  • 2: new Noobs() in line 13.
  • 3: Exception in line 8.
  • 4: new Noobs() in line 14.
  • 5: Exception in line 8.
  • Oh dear, that makes five. Maybe they are ignoring args; then it's four. Remember: two exceptions are created, one for each constructor call.
     
    Will Dev
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is one of the sample mock questions from enthuware.

    so when the object noobs is created, public noobs() is a constructor that is then creating the object new Exception.

    public noobs is not a method...
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not noobs() but Noobs(). You shou‍ld remember the differences between a constructor and a method:-
  • 1: A constructor has exactly the same name as the class.
  • 2: A constructor doesn't have a return type.
  • There are lots of things you can do with methods which are impossible to do with constructors. A constructor is only called when the object is first created.
     
    Will Dev
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Campbell.

    I have a clear understanding now.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!