Private constructor in java example


Constructor in java:

  • Constructor is used to initialize class variables.
  • Constructor will be executed once per object and whenever object is created constructor will be called.




Private Constructor in java:

  • We can make constructor private, public , protected and default.
  • If we define a constructor as private means we are restricting a class to create object  outside of the class.
  • A class having private constructor does not allow user to create object outside.
  • In singleton Design pattern we will use private constructor to restrict object creation


  1. package inheritanceInterviewPrograms;
  2.  
  3. public class A {
  4.   
  5.   private A(){
  6.        
  7.        
  8.    }
  9.  
  10. public static void main(String args[]){ 
  11.    
  12.       A s= new A();
  13.   }
  14.  
  15. }

  1. package inheritanceInterviewPrograms;
  2. //  www.instanceofjava.com
  3.  
  4. public class B {
  5.     
  6. public static void main(String [] args){
  7.      
  8.     A obj= new A();// Compile time error
  9.    
  10.  
  11. }
  12. }


private Constructor example

Super keyword in java inheritance example

  • By using super keyword we can access super class variables and super class methods in sub class.
  • "Super" always refers to super class object.
  • One more interesting point about super is we can call super call constructors from sub class constructor using super keyword.
  • By default in sub class constructor super() call will be added by jvm.
  • We can also explicitly mention super call but rule is super() call must be first statement of the sub class constructor.
  • Let us see how sub class constructor calls super class constructors automatically. 


Java example program to demonstrate super call execution from sub class constructor to super class constructor 


  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. SuperDemo(){
  5. System.out.println("Inside super class constructor");
  6. }
  7.  
  8. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3.  
  4. Subdemo(){
  5. System.out.println("Inside sub class constructor");
  6. }
  7.  
  8. public static void main (String args[]) {
  9.  Subdemo obj= new Subdemo();
  10.  
  11.  
  12. }
  13. }

Output:

  1. Inside super class constructor
  2. Inside sub class constructor


  • In the above programs whenever we are creating object of the sub class corresponding sub class constructor will be executed 
  • By default in sub class constructor super(); call will be added so now it will call super class zero argument constructor.

  1. Subdemo(){
  2. super(); // will be added automatically
  3. System.out.println("Inside sub class constructor");
  4. }

  • Whenever there is no constructor in sub class then default constructor added automatically and that contains super call in it.
  • And also when ever we define a constructor in our class .By default super(); call will be added automatically as first statement.
Java example program to demonstrate super call execution from sub class constructor to super class constructor by placing super call explicitly.


  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. SuperDemo(){
  5. System.out.println("Inside super class constructor");
  6. }
  7.  
  8. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3.  
  4. Subdemo(){
  5. super();
  6. System.out.println("Inside sub class constructor");
  7. }
  8.  
  9. public static void main (String args[]) {
  10.  Subdemo obj= new Subdemo();
  11.  
  12.  
  13. }
  14. }

Output:

  1. Inside super class constructor
  2. Inside sub class constructor

  • Super call must be first statement inside the sub class constructor otherwise compile time error will come.
super keyword in java inheritance


  • If super class having parameterized constructor it is mandatory to call super class constructor explicitly by passing required parameters.
  • other wise compile time error will come

super in inheritance in java



  1. Sub(){
  2. super(1,2); // will work fine
  3. System.out.println("Inside sub class constructor");
  4. }

Java programming interview questions on this keyword part 3



Java Quiz on this keyword part #3

Program #9: Is it possible to Pass this as parameter of a method?

  1. package thiskeywordinterviewprograms.java;
  2.  
  3. public class ThisDemo {
  4.     int a,b;
  5.     
  6. public ThisDemo Show(){
  7.         
  8.   this.a=10;
  9.   this.b=20;
  10.   return this;
  11. }
  12.  
  13. public static void main(String[] args) {
  14.         
  15.     ThisDemo obj = new ThisDemo();
  16.         
  17.   System.out.println("a="+obj.a);
  18.   System.out.println("b="+obj.b);
  19.   ThisDemo obj1 = obj.Show();
  20.         
  21.   System.out.println("a="+obj1.a);
  22.   System.out.println("b="+obj1.b);
  23.         
  24. }
  25.  
  26. }






Program #10: Is is possible to access static variables and static methods using this keyword?

java programming this keyword





Program #11:Java program to test whether we can use this in static block or not?


  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.   static int a,b;
  5.     
  6. static{
  7.     
  8.     this.a=10;
  9.     this.b=20;
  10. }
  11.  
  12. public static void main(String[] args) {
  13.         
  14.   ThisDemo obj = new ThisDemo();
  15.         
  16.   System.out.println("a="+obj.a);
  17.   System.out.println("b="+obj.b);
  18.       
  19.   
  20. }
  21.  
  22. }





Program #12: Java interview program to test whether we can use this in static methods?


  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     static int a,b;
  5.    
  6.  
  7. public static void main(String[] args) {
  8.  

  9.   ThisDemo obj = new ThisDemo();
  10.    this.a=10;
  11.    this.b=20;    
  12.   System.out.println("a="+obj.a);
  13.   System.out.println("b="+obj.b);
  14.       
  15.     
  16. }
  17.  
  18. }







Java interview programming questions on this keyword part 2



Java Quiz on this keyword part #2
  • Java interview program on this keyword: can we call method using this keyword?
  
Program  #5:

java programming on this keyword





Program #6: Can we call non static method from constructor using this?

Java programming examples on this keyword





Program #7: Can we assign something to this ?



  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7.  ThisDemo(int x, int y){
  8.  
  9.        this= new ThisDemo();
  10.         System.out.println("Two argument constructor called.");
  11.         this.x=x;
  12.         this.y=y;
  13.         
  14. }
  15.  
  16. public static void main(String[] args) {
  17.         
  18.     ThisDemo obj = new ThisDemo(10, 20);
  19.         
  20.      System.out.println(obj.a);
  21.      System.out.println(obj.b);

  22. }
  23.  
  24. }





Program #8 : Can we use this as return statement in a method?


  1. public class B{

  2.    int a;
  3.     
  4.  public int getA() {
  5.         return a;
  6.  }
  7.  
  8. public void setA(int a) {
  9.         this.a = a;
  10. }
  11.  
  12. B show(){
  13.     return this;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.        
  18.  B obj = new B();
  19.  
  20.   obj.setA(10);
  21.  
  22.  System.out.println(obj.getA());
  23.  B obj2= obj.show();
  24.  System.out.println(obj2.getA());
  25.  
  26. }

  27. }





Java Interview programming questions on this keyword

  • Lets discuss some interesting programs on this keyword.
  • Now its time to practice some programs on this keyword. 
  • Try to guess the output of the following java programming questions on this keyword.
  • If you want explanations for all the programs please go through this keyword interview questions on java 
  • Some interesting java programming interview questions and answers for freshers and experienced.
Java Quiz on this keyword part #1



Program #1: Java interview programming question on this keyword.


  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     a=a;
  10.     b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo(10, 20);
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }





Program #2: Java interview programming question on this keyword.

  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     this.a=a;
  10.     this.b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo(10, 20);
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }




Program #3: Java Interview programming example on this keyword.

  • What will be the output of the below program

  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     this.a=a;
  10.     this.b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo();
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }





Program #4: Java interview programming question on this keyword.


java programming examples on this keyword





Interview Programming questions on this keyword part 2

Java Program to find shortest palindrome in string

  • We have a Letter or a word then we need add some letters to it and need to find out shortest palindrome 
  • For example we take "S":  S will be the shortest palindrome string.
  • If we take "xyz"zyxyz will be the shortest palindrome string
  • So we need to add some characters to the given string or character and find out what will be the shortest palindrome string by using simple java program.


Java example Program to find out shortest palindrome of given string


  1. package shortestpalindromeexample.java;
  2. import java.util.Scanner;
  3.  
  4. public class ShortestPalindromeDemo {
  5.  
  6. public static String shortestPalindrome(String str) {
  7.      
  8. int x=0;  
  9. int y=str.length()-1;
  10.      
  11.   while(y>=0){
  12.      if(str.charAt(x)==str.charAt(y)){
  13.           x++;
  14.          }
  15.             y--;
  16.   }
  17.  
  18. if(x==str.length())
  19. return str;
  20.  
  21. String suffix = str.substring(x);
  22. String prefix = new StringBuilder(suffix).reverse().toString();
  23. String mid = shortestPalindrome(str.substring(0, x));
  24.  
  25. return prefix+mid+suffix;
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. Scanner in = new Scanner(System.in);
  31.  
  32. System.out.println("Enter a String to find out shortest palindrome");
  33.  
  34. String str=in.nextLine();
  35.  
  36. System.out.println("Shortest palindrome of "+str+" is "+shortestPalindrome(str));
  37.  
  38. }
  39.  
  40. }
Output:

  1. Enter a String to find out shortest palindrome
  2. java
  3. Shortest palindrome of java is avajava

find shortest palindrome in java program

Select Menu