What is Overriding?


When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass.

When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. Methods may be overridden to be more public, not more private.



Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

Top Java Questions


What 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 Strings support 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 its subclasses.
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?

A constructor is a member function of a class that is used to create objects of that class, invoked using the new operator. It has the same name as the class and has no return type. They are only called once, whereas member functions can be called many times. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. Constructor will be automatically invoked when an object is created 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 the super class, we use the super(); statement as the first line of the subclass’s 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 it does not have a modifier it is supposed to have a default access. If a top level class is declared as 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?

Private constructor can be used if you do not want any other class to instantiate the class. This concept is generally used in Singleton Design Pattern. The instantiation of such classes is done from a static public method.

How are this() and super() used with constructors?

this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

What is Method Overriding? What restrictions are placed on method overriding?

When a class defines a method using the same name, return type, and argument list as that of a method in its superclass, the method in the subclass is said to override the method present in the Superclass. When the method is invoked for an object of the
class, it is the new definition of the method that is called, and not the method definition from superclass.
Restrictions placed on method overriding
• Overridden methods must have the same name, argument list, and return type.
• The overriding method may not limit the access of the method it overrides. Methods may be overridden to be more public, not more private.
• The overriding method may not throw any exceptions that may not be thrown by the overridden method.

What are the Object and Class classes used for? Which class should you use to obtain design information about an object?

Differentiate between a Class and an Object?

The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program. The Class class is used to obtain information about an object’s design. A Class is only a definition or prototype of real life object. Whereas an object is an instance or living representation of real life object. Every object belongs to a class and every class contains one or more related objects.

What is a singleton class?

This design pattern is used by an application to ensure that at any time there is only one instance of a class created. You can achieve this by having the private constructor in the class and having a getter method which returns an object of the class and creates one for the first time if its null.What is method overloading and method overriding?

Method overloading: When 2 or more methods in a class have the same method names with different arguments, it is said to be method overloading. Overloading does not block inheritance from the superclass. Overloaded methods must have different method signatures

Method overriding : When a method in a class has the same method name with same arguments as that of the superclass,
it is said to be method overriding. Overriding blocks inheritance from the superclass. Overridden methods must have same signature.

Basically overloading and overriding are different aspects of polymorphism.

static/early binding polymorphism: overloading
dynamic/late binding polymorphism: overriding

If a class is declared without any access modifiers, where may the class be accessed?

A class that is declared without any access modifiers is said to have package or default access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

Does a class inherit the constructors of its superclass?

A class does not inherit constructors from any of its super classes.

Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing

Can an object’s finalize() method be invoked while it is reachable?

An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.

What is the purpose of the Runtime class?

The purpose of the Runtime class is to provide access to the Java runtime system.

It returns the runtime information like memory availability.

* Runtime.freeMemory() –> Returns JVM Free Memory
* Runtime.maxMemory() –> Returns the maximum amount of memory that the JVM will attempt to use. It also helps to run the garbage collector
* Runtime.gc()

What is the purpose of the System class?

The purpose of the System class is to provide access to system resources.

Can an unreachable object become reachable again?

An unreachable object may become reachable again. This can happen when the object’s finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable object.

What is a bean? Where can it be used?

A Bean is a reusable and self-contained software component. Beans created using java take advantage of all the security and platform independent features of java. Bean can be plugged into any software application. Bean is a simple class which has set and get methods. It could be used within a JSP using JSP tags to use them.

What is the functionality of instanceOf() ?
instanceOf opertaor is used to check whether an object can be cast to a specific type without throwing ClassCastException.

What would happen if you say this = null?

It will come up with Error Message

“The left-hand side of an assignment must be a variable”.



Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

Java EE,Open Source,Advanced Technologies Interview Questions


Java EE technologies(erstwhile J2EE).Apart from Java EE, I will also be covering interview questions on most widely used open source technologies like Struts,Hibernate,Spring and Velocity and more.

I have constantly been asked by my readers to put something on personal interview questions, consequently, you may like to read my post on personal interview questions prior talking about Java Enterprise Edition.

Sun Microsystems introduced Java EE 5 specification under JSR 244 and the final release was made public on May ,2006.Java EE has become a cornerstone technology for enterprise level web based solution development.It has more than three million downloads and more than twenty six vendors' application servers support this specification.I believe,it is significant to go through an introduction of Java EE 5 before jumping to questions list straightaway.You can capture essence of Java EE 5 here.

If you directly want to access Master List of Java EE Interview Questions(Release II) then click here.

The list of questions on various topics covered under Java EE hood is as follows(on the basis of different technologies covered under Java EE umbrella) :

Java EE 5(Click To Access Questions' List)
Java Servlets
Java Server Pages and JSP Standard Tag Libraries
Enterprise Java Beans
Java Message Services
Java APIs for XML Parsing
Java Connector Architecture
Java Mail
Java Transaction APIs

Open Source Technologies:
Struts
Hibernate
Spring
Velocity
Apache AXIS
Architectures:
Service Oriented Architecture
Others
Java Design Patterns

I am keen on listening from you all for any kind of feedback,positive or negative.Apart from technical questions,I have prepared a list of personal questions too.I have written in detail about how to approach such questions.The answers to these questions are very critical on your chances to come out with flying colors.

If anyone of you has come across a very unique experience in terms of technical and personal questions asked, feel free to share your experience.


Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

Java Interview Questions


Question: What if the main method is declared as private?

Question: What if the static modifier is removed from the signature of the main method?

Question: What if I write static public void instead of public static void?

Q:Question: What if I do not provide the String array as the argument to the method?

Question: What is the first argument of the String array in main method?

Question: If I do not provide any arguments on the command line, then the String array of Main method will be empty of null?

Question: How can one prove that the array is not null but empty?

Question: What environment variables do I need to set on my machine in order to be able to run Java programs?

Question: Can an application have multiple classes having main method?

Question: Can I have multiple main methods in the same class?

Question: Do I need to import java.lang package any time? Why ?

Question: Can I import same package/class twice? Will the JVM load the package twice at runtime?

Question: What are Checked and UnChecked Exception?

Question: What is Overriding?

Question: What are different types of inner classes?


Q: What if the main method is declared as private?
A: The program compiles properly but at runtime it will give "Main method not public." message.




Q: What if the static modifier is removed from the signature of the main method?
A: Program compiles. But at runtime throws an error "NoSuchMethodError".




Q: What if I write static public void instead of public static void?
A: Program compiles and runs properly.




Q: What if I do not provide the String array as the argument to the method?
A: Program compiles but throws a runtime error "NoSuchMethodError".




Q: What is the first argument of the String array in main method?
A: The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.




Q: If I do not provide any arguments on the command line, then the String array of Main method will be empty of null?
A: It is empty. But not null.




Q: How can one prove that the array is not null but empty?
A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.




Q: What environment variables do I need to set on my machine in order to be able to run Java programs?
A: CLASSPATH and PATH are the two variables.




Q: Can an application have multiple classes having main method?
A: Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.




Q: Can I have multiple main methods in the same class?
A: No the program fails to compile. The compiler says that the main method is already defined in the class.




Q: Do I need to import java.lang package any time? Why ?
A: No. It is by default loaded internally by the JVM.




Q: Can I import same package/class twice? Will the JVM load the package twice at runtime?
A: One can import the same package or same class multiple times. Neither compiler nor JVM complains abt it. And the JVM will internally load the class only once no matter how many times you import the same class.




Q: What are Checked and UnChecked Exception?
A: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method•
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the
exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method• Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.



Q: What is Overriding?
A: When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass.
When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. Methods may be overridden to be more public, not more private.



Q: What are different types of inner classes?
A: Nested -level classes, Member classes, Local classes, Anonymous classes
Nested -level classes- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other -level class.
Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. -level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested -level variety.

Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested -level class. The primary difference between member classes and nested -level classes is that member classes have access to the specific instance of the enclosing class.

Local classes - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a
more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.

Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor


Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

Core Java Interview Questions


Every year many thousand technocrats choose Java careers across the globe,after formally taking Java training classes,courses and/or Java certifications like SCJP etc.While appearing for technical interviews,these budding Java programmers face variety of technical questions, are asked to write code snippets to prove their mettle in relevant topics of Java.They are not only assessed on language basics but also on their familiarity with Java development tools, optimization of Java code,load tests,securing code through Java obfuscation and more. It is their performance in these interviews which decides their career fate and is highly dependent on to-the-point and not too verbose answers.

What prompted me to create a blog on frequently asked and and not so frequently asked tricky Java interview questions? The answer is,as I have been through various technical interviews on Java and related technologies for all my career spent with this fascinating technology so I thought of sharing these questions,code snippets etc. with all.

If you want to test your hands on skills on Java programming, which I am sure most of you have additionally faced during an interview process , then betterprogrammer can help you a great extent.


Apart from technical questions, an interviewee faces a spate of personal questions as well.In my post, I tried to share my views and experience on the Personal Interview Questions.

In wired world, it is very important to know where your resources are lying and how credible are they.Check out this cool widget which provides a quick access to several free online resources which you may find quite useful and handy.Anyone from my audiences is free to use widgets being posted here in one's website/blog :




Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

Related JAVA Questions Posts