What is a class?
A class is a blue print from which individual objects are created.public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }A class can contain any of the following variable types.
- Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
- Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
- Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.
Explain Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. For Detailed tutorials on polymorphism please visit Polymorphism In Java.Explain Method signatures.
The signature of a method is the combination of the method’s name along with the number and types of the parameters (and their order).Why we use static before public static void main?
A static method is one that's associated with a class, but can be called without creating an instance of that class. Most methods (i.e., non-static ones) require an object to be called, but static ones do not.The real answer is, of course, that this is just how they chose to do it. A non-static main() would have worked just as well; the JVM could create an instance of main()'s class to call it if it needed to. But since the JVM is designed to expect a static method, that's what you must use.
Can we have more than one public class in .java file ?
No, we can have only one public class in a .java file. It's just for conventions. Think if you have declared a class as public that means that you want to access it from anywhere else also, now if you declare that class in some other .java file. How does compiler find that, keeping into this mind these conventions are derived.What is enum in java ?
Enumerations (in general) are generally a set of related constants. They have been in other programming languages like C++ from beginning. After JDK 1.4, java designers decided to support it in java also, and it was officially released in JDK 1.5 release.Enumeration in java is supported by keyword enum. enums are a special type of class that always extends java.lang.Enum.
A simple usage will look like this
public enum DIRECTION { EAST, WEST, NORTH, SOUTH //optionally can end with ";" }
Here EAST, WEST, NORTH and SOUTH are final static inner classes of Direction of type Direction extends java.lang.Enum.
Difference between String Buffer and String Builder
Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
Blogger Comment
Facebook Comment