java classes and objects interview questions

Download Java classes and objects interview questions

Post on 25-May-2015




0 download

Embed Size (px)


  • 1. Java Classes and Objects Interview QuestionsWhat restrictions are placed on method overloading?Two methods may not have the same name and argument list but different return types.What is the difference between String and StringBuffer?String objects are immutable whereas StringBuffer objects are not. StringBuffer unlike Stringssupport growable and modifiable strings.Can a private method of a superclass be declared within a subclass?Sure. A private field or method or inner class belongs to its declared class and hides from itssubclasses.There is no way for private stuff to have a runtime overloading or overriding (polymorphism)features.What is the default value of an object reference declared as an instance variable?null unless we define it explicitly.What is the difference between a constructor and a method?OrHow can a subclass call a method or a constructor defined in a superclass?A constructor is a member function of a class that is used to create objects of that class, invokedusing the new operator. It has the same name as the class and has no return type. They are onlycalled once, whereas member functions can be called many times. A method is an ordinarymember function of a class. It has its own name, a return type (which may be void), and isinvoked using the dot operator. Constructor will be automatically invoked when an object iscreated whereas method has to be called explicitly.super.method(); is used to call a super class method from a sub class. To call a constructor of thesuper class, we use the super(); statement as the first line of the subclasss constructor.Can a top-level class be private or protected?No. A top-level class cannot be private or protected. It can have either "public" or no modifier. If itdoes not have a modifier it is supposed to have a default access. If a top level class is declaredas private/protected the compiler will complain that the "modifier private is not allowed here.Why Java does not support multiple inheritance?Java does support multiple inheritance via interface implementation.Where and how can you use a private constructor?