Consider the following example class Student { public void doSomeTask() { System.out.println("Doning Task!!"); } public static void main(String args[]) { Student obj = new Student(); obj.doSomeTask(); } } If you notice the code in public static void main then y...

Read More

Unlike C++, Java doesn’t create a default copy constructor if we don’t write our own. But, Java supports copy Constructors. Copy constructor provides the facility to create an object from another object of same type received as constructor argument. There are many ways to copy the values of one object into another in java. They are: By constructor ...

Read More

This is a most frequently asked java interview question. The answer is No, interface cannot have constructors. Now let us check by an example that why constructors in interface are not allowed in Java. Interface methods are by default public and abstract. It is the responsibility of the implementing class to define the body of the method. For e.g. ...

Read More

Like method overloading we can overload constructors also. We can have multiple constructors in a class. Along with default constructor, we can have constructors with parameters. The no of parameters can be same, and it can have different datatypes. Below example gives sample code for constructors overloading.Following is an Example: package com....

Read More

No, static constructors are not allowed in Java. Lets check from the example why? public class Student { /* below is a static constructor */ public static Student() { System.out.println("Static Constructor of the class"); } public static void main(String args[]) { /* Now we are trying to crea...

Read More

Instance initializer blocks are simply a block of code {} which can be used to do execute some pre-initialization logic or just initialize certain fields before they are used. An example would be the following. class A{     int a;     double b=Math.random();     {         if (b>0.5)             a=1;         else             a=-1;     }  } The above...

Read More

An interface in java is a blueprint of a class. It defines what the class will do. The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java. An interface represents an IS-A relationship. A clas...

Read More

Marker or Tag interfaces are interfaces which do not have any method declaration in them. Natural question is why would anyone need such interfaces. These are mainly required by JVM/Compiler to identify certain object types which require specific handling. Some examples are Serializable, Cloneable, Remote etc. You can also create your own marker in...

Read More

Java 8 permits static interface methods: With Java 8, interfaces can have static methods. They can also have concrete instance methods, but not instance fields. There are really two questions here: Why, in the bad old days, couldn't interfaces contain static methods? Why can't static methods be overridden? Static methods in interfaces There was no ...

Read More

An interface can be declared as a member of a class or another interface. Such an interface is called a member interface or nested interface. A nested interface can be declared as public, private or protected as opposed to a top-level interface which must be public or default. When a nested interface is used outside of its enclosing scope, it must ...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself