What is the main difference in object creation between Java and C++? -
What is the main difference in object creation between Java and C++? -
i'm preparing exam in java , 1 of questions on previous exam was:"what main difference in object creation between java , c++?"
i think know basics of object creation illustration how constructors called , initialization blocks in java , happens when constructor of 1 class calls method of class isn't constructed yet , on, can't find obvious. reply supposed 1 or 2 sentences, don't think description of whole object creation process in java had in mind.
any ideas?
in add-on other first-class answers, 1 thing important, , ignored/forgotten, or misunderstood (which explains why detail process below):
in java, methods virtual, when called constructor (which lead bugs) in c++, virtual methods not virtual when called constructor (which lead misunderstanding) what? let's imagine base of operations class, virtual method foo(). let's imagine derived class, inheriting base, overrides method foo()the difference between c++ , java is:
in java, calling foo() base of operations class constructor phone call derived.foo() in c++, calling foo() base of operations class constructor phone call base.foo() why?the "bugs" each languages different:
in java, calling method in constructor lead subtle bugs, overridden virtual method seek access variable declared/initialized in derived class.conceptually, constructor’s job bring object existence (which hardly ordinary feat). within constructor, entire object might partially formed – can know base-class objects have been initialized, cannot know classes inherited you. dynamically-bound method call, however, reaches “forward” or “outward” inheritance hierarchy. calls method in derived class. if within constructor, phone call method might manipulate members haven’t been initialized yet – sure recipe disaster.
bruce eckel, http://www.codeguru.com/java/tij/tij0082.shtml
in c++, 1 must remember virtual won't work expected, method of current constructed class called. reason avoid accessing info members or methods not exist yet.during base of operations class construction, virtual functions never go downwards derived classes. instead, object behaves if of base of operations type. informally speaking, during base of operations class construction, virtual functions aren't.
scott meyers, http://www.artima.com/cppsource/nevercall.html
java c++ object creation
Comments
Post a Comment