Multiple inheritance in java

Multiple inheritance in java can be achieved by following ways: A class can implements multiple interfaces. An interface can extends multiple interfaces. Example interface DisplayAge{ void displayAge(int age); }   interface DisplayName{ void displayName(String name); }   public class Test implements DisplayAge, DisplayName{   @Override public void displayAge(int age) { System.out.println("Age = " + age); … Read more

Why multiple inheritance is not supported java

In java, multiple inheritance is not supported because of ambiguity problem. We can take the below example where we have two classes Class1 and Class2 which have same method display(). If multiple inheritance is possible than Test class can inherit data members (properties) and methods (behaviour) of both Class1 and Class2 classes. Now, Test class … Read more

Static method in java

Static methods Static methods symbolize the behavior of entire class. An instance of a class just isn’t required to execute static methods. They are often called using class name. Syntax: ClassName.methodNameClassName.methodName Example class DisplayTest { public static void display(){ System.out.println("Hello World"); } } public class StaticMethodExample { public static void main(String args[]){ DisplayTest.display(); } }class … Read more

Static variable in java

Static data members Data members declared with static keyword are generally known as static data members. These are primarily used to characterize these properties that are common to each object. On the time of class loading a single copy is created for static data members and it will be shared by all objects. Memory division … Read more

how to open a notepad in java?

package com.w3schools;   import java.io.IOException;   public class Test { public static void main(String args[]){ try { Runtime.getRuntime().exec("notepad.exe"); } catch (IOException e) { e.printStackTrace(); } } }package com.w3schools; import java.io.IOException; public class Test { public static void main(String args[]){ try { Runtime.getRuntime().exec("notepad.exe"); } catch (IOException e) { e.printStackTrace(); } } }

can a constructor call another constructor java?

Yes, a constructor can be called by another constructor in java. We can use this() to call same class constructor and super() to call super class constructor. class SuperClass{ public SuperClass(int i){ System.out.println("Super class constructor"); } }   class SubClass extends SuperClass { public SubClass(){ //Calling same class constructor this(50); }   public SubClass(int i){ … Read more

Private constructor in java

We can create private constructor in java. It is used to restrict the instantiation of a class. We cannot create an object outside of the class, if we create the private constructor. It is used to implement Singleton pattern. The main purpose of singleton pattern is to control object creation i.e. keep only one instance … Read more

What happens if you keep return type for a constructor?

As we discussed in previous questions that we can overload a constructor so if we keep return type for a constructor it will be treated as a normal method. Note: Compiler gives a warning message that method has a constructor name. Example public class Main { String Main(){ System.out.println("Hello World"); return "Hello w3schools.com"; } public … Read more