Constructor in interface ?

Can we write constructor inside interface in java?

  • No. Interfaces does not allow constructors.
  • Why interface does not have constructor? The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.
  • what happens when a constructor is defined for an interface



What will happens if we try to create constructor inside interfaces in java

  • If we try to create constructor in interface compile time error will come.
  • Error description: Interfaces cannot have constructors.


  1. public interface sample{
  2.  
  3.  int a=10;

  4. sample(){//Interfaces cannot have constructors.
  5.  
  6. }




constructor in interface java





Interfaces in Java 8:

  • Before java 8 interfaces allows only public abstract methods.
  • If we declare any method in interface with default it will be treated as public abstract method.
  • Interface methods doesn't have body. The class which implements interfaces are responsible for implementing unimplemented methods of interface.
  • But in Java 8 static and default methods added.

Default methods:

  • Defaults methods are also  known as defender methods or virtual extension methods
  • Default methods will help us to avoid utility classes.
  • We can define utility methods inside the interface and use it in all classes which is implementing.
  • One of the major reason to introduce this default methods in java 8 is to support lambda expressions in collections API and to enhance.


  1. package com.instanceofjava;
  2. interface Java8InterfaceDemo{
  3.  
  4. abstract void print();
  5.   
  6. default void display(){
  7.  
  8. System.out.println("default method of interface");
  9.  
  10. }
  11.  
  12. }

  1. package com.instanceofjava;
  2. class Sample implements Java8InterfaceDemo{
  3.  
  4. void print(){
  5. System.out.print("overridden method ")
  6.  }
  7. public static void main(String[] args){
  8.   
  9. Sample obj= new Sample();
  10.  
  11. obj.print(); // calling implemented method
  12. obj.display(); // calling inherited method
  13.  
  14. }
  15.  
  16. }

Output:

  1. overridden method
  2. default method of interface
  3. default method of interface

Static methods in Java 8:

  • These static method will act as helper methods.
  • These methods are the parts of interface not belongs to implementation class objects.

  1. package com.interfacesinJava8;
  2. interface StaticInterface{
  3.  
  4. Static void print(String str){
  5.  
  6. System.out.println("Static method of interface:"+str);
  7.  
  8. }
  9. }

  1. package com.instanceofjava;
  2. class Demo implements StaticInterface{
  3.  
  4. public static void main(String[] args){
  5.   
  6.  StaticInterface.print("Java 8")
  7.  
  8. }
  9.  
  10. }

Output:

  1. Static method of interface: Java 8

Can we create private constructor in java

1.Can a constructor in Java be private?
  • Yes we can declare private constructor in java.
  • If we declare constructor as private we can not able to create object of the class.
  • In singleton design pattern we use this private constructor. 

private constructor in java example program


 2.In what scenarios we will use private constructor in java.

  •  Singleton Design pattern
  • It wont allow class to be sub classed.
  • It wont allow to create object outside the class.
  • If All Constant methods is there in our class we can use private constructor.
  • If all methods are static then we can use private constructor.



 3.What will happen if we extends a class which is having private constructor.

  •  If we try to extend a class which is having private constructor compile time error will come.
  • Implicit super constructor is not visible for default constructor. Must define an explicit constructor

private constructor in java


Singleton Design pattern:


  1. package com.privateConstructorSingleTon;
  2.  
  3. public class SingletonClass {

  4. private static SingletonClass object;
  5.  
  6. private SingletonClass ()
  7. {
  8.         System.out.println("Singleton(): Private constructor invoked");
  9. }
  10.  
  11. public static SingletonClass getInstance()
  12. {
  13.  
  14. if (object == null)
  15. {
  16.  
  17. System.out.println("getInstance(): First time getInstance was called and object created !");
  18. object = new SingletonClass ();
  19.  
  20.  }
  21.  
  22. return object;
  23.  
  24. }
  25.  
  26. }
     



  1. package instanceofjava;
  2.  
  3. public class SingletonObjectDemo {

  4. public static void main(String args[]) {
  5.  
  6.      SingletonClass s1= SingletonClass .getInstance();
  7.      SingletonClass s2= SingletonClass .getInstance();
  8.      System.out.println(s1.hashCode());
  9.      System.out.println(s2.hashCode());
  10.  
  11. }
  12. }

Output:

  1. getInstance(): First time getInstance was called and object created !
  2. Singleton(): Private constructor invoked
  3. 655022016
  4. 655022016

Java Static Constructor : is it possible to create static constructor?

1.Is it possible to create static constructor in java?
  • No. We can not create constructor with static.
  • If we try to create a static constructor compile time error will come: Illegal modifier for the constructor
Static construcotr in java interview question



2.What is the real use of constructor in java? 

  • Constructors will be used to assign instance variables with default values.
  • Whenever object is created constructor will be called so the default values for the instance variables will be assigned in this constructor.
  • Top 15 Java Interview Questions on Constructors

  1. public class ConstructorDemo {
  2.  
  3.  int a,b;

  4. ConstructorDemo (int x, int y){
  5.  
  6. a=x;
  7. b=y;
  8.  
  9. }
  10. public static void main(String[] args) {
  11.  
  12.         ConstructorDemo ob= new ConstructorDemo ();
  13.  
  14.     }
  15. }


3. What is the use of static in java?
  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.
4.Is there any alternative solution for static constructor in java

  • Static means class level.
  • Constructor will be use to assign initial values for instance variables
  • static and constructor are different and opposite from each other.
  • To assign initial values for instance variable we use constructor.
  • To assign static variables we use Static Blocks
  • We can use static blocks to initialize static variables in java.


5.what is static block in java?

  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }
6. write a java program to assign static variables using static block


  1. package com.staticInitializer;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

  1.Static Variables 

  2.Static Methods 

  3.Static Blocks

  4.Main method

How to check internet connection using java

  • Hi friends now we are going to check our pc is having internet connection or not using java code 
  • yes we can check internet connection using java code java.net package providing some useful classes.
  • Using java.net.URL and java.net.URLConnection classes we need to check we can check we have a connection or not.
  • Lets see the program to detect internet connection using java
#1:


  1. package InternetConnections;
  2. import java.net.URL;
  3. import java.net.URLConnection; 

  4. public class CheckInternetConnection{
  5.  
  6. public static void main(String[] args){
  7.   
  8. try 
  9. {
  10.         URL url = new URL("http://www.instanceofjava.com");
  11.  
  12.         URLConnection connection = url.openConnection();
  13.         connection.connect();   
  14.  
  15.         System.out.println("Internet Connected");   
  16.             
  17.  }catch (Exception e){
  18.      
  19. System.out.println("Sorry, No Internet Connection");     
  20.                                                             

  21. }
  22. }

Detect internet connection using java program



#2:
  • By using  getRuntime() method of java.lang.Runtime  we can also test internet connection in java
  • The output will be 0 if internet connection available 1 if not.


how to check internet connection in java



#3:


  1. package InternetConnections;
  2.  
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.util.Enumeration;

  6. public class CheckInternetConnection{
  7.  
  8. public static void main(String[] args){
  9.  
  10. Enumeration<NetworkInterface> interfaces = null;
  11.  
  12. try {
  13.            
  14.      interfaces = NetworkInterface.getNetworkInterfaces();
  15.  
  16.   } catch (SocketException e) {
  17.             e.printStackTrace();
  18.  }  
  19.  
  20.  while (interfaces.hasMoreElements()) {  
  21.  
  22.       NetworkInterface nic = interfaces.nextElement();   
  23.       System.out.print("Interface Name : [" + nic.getDisplayName() + "]");  
  24.      
  25. try {
  26.       
  27.  System.out.println(", is connected : [" + nic.isUp() + "]"); 

  28. } catch (SocketException e) {
  29.                 e.printStackTrace();
  30. }  
  31.  
  32. }   

  33. }
  34. }



Output:

  1. Interface Name : [Software Loopback Interface 1], is connected : [true]
  2. Interface Name : [WAN Miniport (SSTP)], is connected : [false]
  3. Interface Name : [WAN Miniport (L2TP)], is connected : [false]
  4. Interface Name : [WAN Miniport (PPTP)], is connected : [false]
  5. Interface Name : [WAN Miniport (PPPOE)], is connected : [false]
  6. Interface Name : [WAN Miniport (IPv6)], is connected : [false]
  7. Interface Name : [WAN Miniport (Network Monitor)], is connected : [false]
  8. Interface Name : [WAN Miniport (IP)], is connected : [false]
  9. Interface Name : [RAS Async Adapter], is connected : [false]
  10. Interface Name : [WAN Miniport (IKEv2)], is connected : [false]
  11. Interface Name : [Realtek PCIe GBE Family Controller], is connected : [true]
  12. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter], is connected : [false]
  13. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  14. Interface Name : [Microsoft Virtual WiFi Miniport Adapter], is connected : [true]
  15. Interface Name : [null], is connected : [false]
  16. Interface Name : [Microsoft Virtual WiFi Miniport Adapter #2], is connected : [false]
  17. Interface Name : [null], is connected : [false]
  18. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  19. Interface Name : [Apple Mobile Device Ethernet], is connected : [false]
  20. Interface Name : [Microsoft 6to4 Adapter], is connected : [true]
  21. Interface Name : [Microsoft ISATAP Adapter #4], is connected : [false]
  22. Interface Name : [Microsoft ISATAP Adapter], is connected : [false]
  23. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify WLAN
  24. LightWeight Filter-0000], is connected : [false]
  25. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify NDIS
  26. LightWeight Filter-0000], is connected : [false]
  27. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet
  28. Scheduler0000], is connected : [false]
  29. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter
  30. Driver-0000], is connected : [false]
  31. Interface Name : [Realtek PCIe GBE Family Controller-Connectify NDIS LightWeight Filter
  32. 0000], is connected : [false]
  33. Interface Name : [Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000], is
  34. connected : [false]
  35. Interface Name : [Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000], is
  36. connected : [false]
  37. Interface Name : [Realtek PCIe GBE Family Controller-Deterministic Network Enhancer
  38. 0000], is connected : [false]
  39. Interface Name : [WAN Miniport (IPv6)-Connectify NDIS LightWeight Filter-0000], is
  40. connected : [false]
  41. Interface Name : [WAN Miniport (IPv6)-Deterministic Network Enhancer-0000], is connected
  42. : [false]
  43. Interface Name : [WAN Miniport (IPv6)-QoS Packet Scheduler-0000], is connected : [false]
  44. Interface Name : [WAN Miniport (IP)-Connectify NDIS LightWeight Filter-0000], is
  45. connected : [false]
  46. Interface Name : [WAN Miniport (IP)-Deterministic Network Enhancer-0000], is connected :
  47. [false]
  48. Interface Name : [WAN Miniport (IP)-QoS Packet Scheduler-0000], is connected : [false]
  49. Interface Name : [WAN Miniport (Network Monitor)-Connectify NDIS LightWeight Filter
  50. 0000], is connected : [false]
  51. Interface Name : [WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000], is
  52. connected : [false]
  53. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter
  54. Driver-0000], is connected : [false]
  55. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Deterministic Network
  56. Enhancer-0000], is connected : [false]
  57. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-WFP LightWeight
  58. Filter-0000], is connected : [false]
  59. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000], is
  60. connected : [false]
  61. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Deterministic Network Enhancer
  62. 0000], is connected : [false]
  63. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify NDIS LightWeight
  64. Filter-0000], is connected : [false]
  65. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-QoS Packet Scheduler-0000], is
  66. connected : [false]
  67. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-WFP LightWeight Filter-0000], is
  68. connected : [false]
  69. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify WLAN LightWeight
  70. Filter-0000], is connected : [false]

Top 10 Interview Programs and questions on method overriding in java

1.What is method overriding in java?
  • Defining multiple methods with same name and same signature in super class and sub class known as method overriding. 
  • Method overriding is type of polymorphism in java which is one of the main object oriented feature.
  • Redefined the super class method in sub class is known as method overriding.
  • method overriding allows sub class to provide specific implementation that is already defined in super class.
  • Sub class functionality replaces the super class method functionality (implementation).
2. Can we override private methods in java?



3. Can we override static methods of super class in sub class?
  • NO.Its not possible to override static methods because static means class level so static methods not involve in inheritance.

 4. Can we change the return type of overridden method in sub class?
  • No. Return type must be same in super class and sub class.


  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }



  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. int add(){    //Compiler Error: The return type is incompatible with Super.add()
  5.  
  6. System.out.println("Sub class add method");
  7. return 0; 

  8. }

  9. }

5.Can we change accessibility modifier in sub class overridden method?
  • Yes we can change accessibility modifier in sub class overridden method but should increase the accessibility if we decrease compiler will throw an error message.


Super class method  Subclass method
protected protected, public
public      public
default     default , public
6.What happens if we try to decrease accessibility from super class to sub class?
  • Compile time error will come.
  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add(){ //Compiler Error: Cannot reduce the visibility of the inherited method
  5. from Super
  6.  
  7. System.out.println("Sub class add method");

  8. }

  9. }


method overriding example program interview question


7.Can we override a super class method without throws clause to with throws clause in the sub class?

  • Yes if super class method throws unchecked exceptions.
  • No need if super class method throws checked exceptions. But it is recommended to add in sub class method in order to maintain exception messages.

 8.What are the rules we need to follow in overriding if super class method throws exception ?


 9.What happens if we not follow these rules if super class method throws some exception.

  •  Compile time error will come.

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws Exception{ //Compiler Error: Exception Exception is not compatible with
  5. throws clause in Super.add()
  6. System.out.println("Sub class add method");

  7. }

  8. }

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws NullPointerException{ // this method throws unchecked exception so no
  5. isuues
  6. System.out.println("Sub class add method");

  7. }

  8. }

10.Can we change an exception of a method with throws clause from unchecked to checked while overriding it?
  •  No. As mentioned above already
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.
  • So we can not change from unchecked to checked 

 You might Like:

 

Top 15 Java abstract class and abstract method interview questions and programs

1.What is abstract class in java?
  • Hiding the implementation  and showing the function definition to the user.
  • Abstract class contains abstract methods and concrete methods(normal methods)
2.How can we define an abstract class? 
  • Using abstract keyword we can define abstract class.
  • Check below code for abstract class example program. 

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   //
  5. }




3. How to declare an abstract method?

  • By Using abstract keyword in the method signature we can declare abstract method.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   abstract void show(); 

  5. }

 4. Can we define abstract class without abstract method?

  •  Yes we can define abstract class without abstract methods.
  • It is not mandatory to create at-least one abstract method in abstract class.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.  

  5. }

 Read more at Can you define an abstract class without any abstract methods? if yes what is the use of it?


 5.Can we create object object for abstract class?
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly.
  • Through sub class abstract class members will get memory. Whenever we create sub class object of abstract class  abstract class object will be created. i.e abstract class members will get memory at that time.


  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractClass obj= new AbstractClass (); 
  13. // ERROR: Cannot instantiate the type AbstractClass
  14.  
  15. }
  16. }

 6. Is is possible to declare abstract method as static?
  • No. its not possible to declare abstract method with static keyword.
  • If we declare abstract method with static compiler will throw an error.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


abstract class interview questions


 7.Can we declare abstract method as final?
  • No. its not possible to declare abstract method with final keyword.
  • If we declare abstract method with final compiler will throw an error.
  • Because abstract methods should be override by its sub classes.


  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



abstract class interview programs


8. Is it possible to declare abstract method as private?
  • No. its not possible to declare abstract method with private .
  • If we declare abstract method with private compiler will throw an error.
  • Because abstract methods should be override by its sub classes.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     private abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
9. Is it possible to declare abstract method as public ?
  • Yes we can declare abstract methods as public.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     public abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
10. Is it possible to declare abstract method with default?
  • Yes we can declare abstract methods with default.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.    abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

11. Is it possible to declare abstract method with protected modifier?
  • Yes we can declare abstract methods as protected.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     protected abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 

12.What are the valid and invalid keywords or modifier with abstract class?
  • public,  protected and default are valid.
  • static,  final and private are invalid.

13.Can abstract method declaration include throws clause?
  • Yes. We can define an abstract class with throws clause.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     abstract void add() throws Exception; 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
 14.What happens if sub class not overriding abstract methods?
  • If sub class which is extending abstract class not overriding abstract method compiler will throw an error.
  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public class Sample extends AbstractClass {
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }
15. Can we escape of overriding abstract class in sub class which is extending abstract class?
  • Yes we can escape from overriding abstract method from super abstract class by making our class again as abstract.
  • The class which is extending our class will get responsibility of overriding all abstract methods in our class and in super class.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public abstract class Sample extends AbstractClass {
  3.  
  4.    
  5. }

  1. package Abstraction;
  2. public class Example extends Sample{
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }

You Might Like:



Can we call super class static method from subclass in java

  • If you want to call static method of a class we can call directly from another static method or by using its class name we can call static method of that class.
  • let us see a program on how to call a static method of a class

  1. package com.instanceofjava;
  2. public class Sample{

  3. public static void show(){
  4.  
  5.  System.out.println("show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10.  show();
  11. Sample.show();
  12.  
  13. }
  14. }

  • We can also call static method like this but this is not recommended.

static method in java interview questions




 Output:
  1. show() method called
  2. show() method called

  • Now our question is can we call super class static method from sub class?
  • Yes we can call super class static method inside sub class using super_class_method();
  • We can also call super class static method using Sub_class_name.superclass_staticMethod()


  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public static void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }



  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void print(){
  4.  
  5.  System.out.println("Sub class print() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo.show();
  11. SubDemo.show();
  12. }
  13. }

Output:
  1. Super class show() method called
  2. Super class show() method called

  • If the same static method defined in sub class also then we can not call super class method using sub class name if we call them sub class static method will be executed.

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public static void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public static void show(){
  4.  
  5.   System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo.show();
  11. SubDemo.show();
  12. }
  13. }

 Output:
  1. Super class show() method called
  2. Sub class show() method called

Key points to remember:
  1. Can we Overload static methods in java
  2. Can we Override static methods in java

Inheritance interview programming questions : Part 2

1. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13. }
  14. }






2. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }



  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13.  SubDemo subobj=new SubDemo();
  14.  subobj.show();
  15.  
  16.  }
  17. }





3. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SubDemo();
  11. supobj.show(); 
  12.  
  13. }
  14. }





4. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10.  public void superMethod(){
  11.  
  12.         System.out.println("Super class superMethod() called");
  13.  
  14. }
  15.  
  16. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7.  
  8. public void subMethod(){
  9.         System.out.println("Sub class subMethod() called");
  10. }

  11.  public static void main(String args[]){
  12.  
  13. SuperDemo supobj= new SubDemo();
  14. supobj.superMethod();
  15.  
  16. // supobj.subMethod();  // compile time error: The method subMethod() is undefined for the
  17. type Super

  18.  ((SubDemo)supobj).subMethod();
  19. ((SuperDemo)supobj).show();
  20. }
  21. }







Constructor chaining in java using this and super Keywords

Constructor chaining example program in same class using this keyword.


  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2

Constructor chaining example program in same class using this and super keywords.

  1. package instanceofjava;
  2. class SuperClass{
  3.  
  4. SuperClass(){
  5. this(1);
  6.  System.out.println("Super Class no-argument constructor");
  7.  
  8.  
  9. SuperClass(int x ){
  10.  
  11. this(1,"constructor chaining"); 
  12. System.out.println("Super class one -argument constructor(int)");
  13.  
  14. }
  15.  
  16. SuperClass(int x , String str){
  17.  System.out.println("Super class two-argument constructor(int, String)");
  18.  
  19. }




  1. package instanceofjava;
  2. class SubClass extends SuperClass{

  3. SubClass(){
  4. this(1);
  5.  System.out.println("Sub Class no-argument constructor");
  6.  
  7.  
  8. SubClass(int x ){
  9.  
  10. this("Constructor chaining"); 
  11.  System.out.println("Sub class integer argument constructor");
  12.  
  13. }
  14.  
  15. ConstructorChaining(String str){
  16. //here by default super() call will be there so it will call super class constructor
  17.   System.out.println("Sub class String argument constructor");
  18.  
  19. public static void main(String[] args){
  20.  
  21.  SubClass obj=new SubClass();
  22.  
  23.  
  24. }
  25. }

Output:

  1. Super class two-argument constructor(int, String)
  2. Super class one -argument constructor(int)
  3. Super Class no-argument constructor
  4. Sub class String argument constructor
  5. Sub class String argument constructor
  6. Sub class integer argument constructor




Java Coding Interview programming Questions : Java Test on HashMap

1.What will be the Output of this program.


  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.     System.out.println(hm.size()); 
  11.  
  12. for (Integer name: hm.keySet()){
  13.  
  14.    System.out.println(name + " " + hm.get(name)); 
  15.   
  16. }
  17.  
  18. }
  19. }





2.Basics of java programming : Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.  
  12.     System.out.println(hm.size()); 
  13.  
  14. for (Integer name: hm.keySet()){
  15.  
  16.    System.out.println(name + " " + hm.get(name)); 
  17.   
  18. }
  19.  
  20. }
  21. }









3.Java programming examples: Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.     System.out.println(hm.size()); 
  14.  
  15. for (Integer name: hm.keySet()){
  16.  
  17.    System.out.println(name + " " + hm.get(name)); 
  18.   
  19. }
  20.  
  21. }
  22. }






4.Java programming for beginners : HashMap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.      Integer i=null;
  14.      String str="java programming basics Interview questions";
  15.  
  16.      hm.put(i, str);
  17.  
  18.     System.out.println(hm.size()); 
  19.  
  20. for (Integer name: hm.keySet()){
  21.  
  22.    System.out.println(name + " " + hm.get(name)); 
  23.   
  24. }
  25.  
  26. }
  27. }





Find Second smallest number in java without sorting

Java interview Program to find second smallest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. int[] x ={10,11,12,13,14,6,3,-1};
  5.  
  6.         int small=x[0];
  7.  
  8.  for(int i=0;i<x.length;i++)
  9.  {
  10.         if(x[i]<small)
  11.         {
  12.         small=x[i];
  13.         }
  14.  }
  15.  
  16.    int sec_Small=x[0];
  17.  
  18. for(int i=0;i<x.length;i++)
  19.  {
  20.         if(x[i]<sec_Small && x[i]!=small)
  21.         {
  22.         sec_Small=x[i];
  23.         }
  24.   }
  25.  
  26.         System.out.println("Second Smallest Number: "sec_Small);
  27.         }
  28. }



Output:
 
  1. Second Smallest Number:3

Java interview  Program to find second Smallest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Smallest Number: "+numbers[0]);
  11.   System.out.println("Second Smallest Number: "+numbers[1]);

  12.  }
  13.  
  14. }




Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5



Find second highest number in an integer Array

Java Interview Program to find second highest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7. int highest = 0;
  8.  int second_highest = 0;
  9.  
  10. for(int n:numbers){
  11.  
  12. if(highest < n){
  13.  
  14.       second_highest = highest;
  15.       highest =n;
  16.  
  17.  } else if(second_highest < n){
  18.  
  19.                 second_highest = n;
  20.  
  21. }
  22.  
  23. }
  24.         System.out.println("First Max Number: "+highest);
  25.         System.out.println("Second Max Number: "+second_highest);

  26.  
  27.  }
  28.  
  29. }



Output:
 
  1. First Max Number: 64
  2. Second Max Number: 46

Java Interview Program to find second highest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Largest Number: "+numbers[numbers.length-1]);
  11.   System.out.println("Second Largest Number: "+numbers[numbers.length-2]);
  12.  }
  13.  
  14. }




Output:
 
  1. Largest Number: 64
  2. Second Largest Number: 46



How to find Biggest Substring in between specified character or String

1. Java Program to find biggest substring in between specified character or string

String: i am rajesh kumar ravi

in between: 'a'

 

  1. package com.instaceofjava;
  2.  
  3. public class BiggestSubString{
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="i am rajesh kumar ravi";
  8.  
  9.  String longest="";
  10.  int maxlength=0;
  11.  
  12.  String arr[]=str.split("a");
  13.  
  14. for (int i = 1; i < arr.length-1; i++) {
  15.  
  16.    System.out.println(arr[i]); // printing substrings
  17.  
  18.           if(arr[i].length() > maxlength){
  19.                maxlength = arr[i].length();
  20.                 longest = arr[i];
  21.             }   
  22.  
  23.    }      
  24.   
  25. System.out.println("Longest substring is: "+longest);
  26.  
  27. }
  28. }


Output:

  1. m r
  2. jesh kum
  3. r r
  4. Longest substring is: jesh kum


Java Basic Interview Programming Questions on Constructors

  • Below programs are the basic programs frequently asking in java quizzes  try to test your java programming skills by answering the questions. if you are having any doubts feel free to ask us.
  • Java Coding Questions on constructors.


1.What will be the Output of this program.

  1. public class Test1{
  2.  
  3. Test1(int i){
  4.  
  5. System.out.println("Test1 Constructor "+i);
  6.  
  7.  } 
  8. }

  1. public class Test2{
  2.  
  3. Test1 t1= new Test1(10);
  4.  
  5. Test2(int i){
  6.  
  7.  System.out.println("Test2 Constructor "+i);
  8.  

  9. public static void main(String[] args) {
  10.  
  11.         Test2 t2= new Test2(5);
  12.  
  13.     }
  14. }







2.What will be the Output of this program.

  1. public class A{
  2.  
  3. A(){
  4.  
  5.   System.out.println("A Class Constructor ");
  6.  
  7. }
  8.  
  9. }

  1. public class B extends A{
  2.  
  3. B(){
  4.  
  5.   System.out.println("B Class Constructor ");
  6.  
  7. }
  8. public static void main(String[] args) {
  9.  
  10.         B ob= new B();
  11.  
  12.     }
  13. }







3.What will be the Output of this program.

  1. public class  A{
  2.  
  3. A(){

  4. this(0);
  5. System.out.println("Hi ");
  6.  
  7.  
  8. A(int x){

  9. this(0,0);
  10. System.out.println("Hello");
  11.  
  12. }
  13.   
  14. A(int x, int y){
  15.  
  16. System.out.println("How are you");
  17.  
  18. }
  19. public static void main(String[] args) {
  20.  
  21.         A ob= new A();
  22.  
  23.     }
  24. }




Select Menu