Inheritance programming interview questions

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 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 method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  SubDemo subobj=new SubDemo();
  10.  subobj.show();
  11. }







2. what is the output of following program:

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

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.  super.x=y+2;
  9.   System.out.println("x="+super.x+"y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }




3. what is the output of following program:

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

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2.  
  3. public class SubDemo extends SuperDemo{
  4.  
  5.  int y;
  6.  
  7. public void show(){
  8.  
  9.  super.x=y+2;
  10.   System.out.println("x="+super.x+"y="+y);
  11.  
  12. }
  13.  
  14.  public static void main(String args[]){
  15.  
  16.  SubDemo subobj=new SubDemo();
  17.  subobj.x=2;
  18.  subobj.y=2;
  19.  subobj.show();
  20.  
  21. }




4. what is the output of following program:

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

  4.  int x;
  5. SuperDemo(){
  6.  
  7.  x=24;

  8. }
  9. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.   System.out.println("x="+super.x);
  9.   System.out.println("y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }





Arrays in java

Array:

  • Array is referenced data type used to store multiple values.
  • we can not change size of array at runtime.
  • memory can not be altered Once the memory has been allocated for an array at the run time.
  • Arrays are objects


Need of array:

  • In  real time projects array is used to collect same type of objects to send all values with single method call.

Problem of primitive data types:

  • We can not store values in continuous memory locations using primitive data types .
  • We have two problems due to this limitation

1.we cant store multiple values:

  • If we want to store multiple values , say 1 to 10 , we must create 10 variables .
  • All those 10 variables are created at different locations.

2.In single method call we can not pass multiple values :

  • we can not pass all values to the remote computer with single network call or method call.Using primitive variables  , which increases burden on network and also increase number of lines of code in program.

Solution:

  • Values must be stored in continuous memory locations with single variable name. To solve above two problems ,
  • This can be possible using array.
  • In java array is reference data type . it is used to store fixed number of multiple values of same type in continuous memory locations.
  • Like other data types array is not a keyword it is a concept. it creates continuous memory locations using other primitive or reference types.

 Array Limitation: 

  •  Array size is fixed , means we can not increase or decrease its size after its creation.

Array Declaration:

  • <Accessibility modifier><Modifier><datatype>[] <array variable name>;

For example:

  • public static int[] i;
  • public static Example[] e;
  • Like in C or C++ , in java we can not mention array size in declaration part. It leads Compile time error.
  • int[5] i; Compile time Error: illegal start of expression
  • int [] i;

Possible declaration:

  1. After data type : int[] i;
  2. Before variable name : int []i;
  3. After variable name : int i[];

Type of arrays:

  1.  Single dimensional : int [] i;
  2. Two dimensional : int[][] i;
  3. Three dimensional : int [][][] i;

Array object creation:

  • <Accessibility Modifier><Modifier><data type>[]<array name>={<list of values with , separator>};
  • int [] ia={10,20,30,40};

Super keyword

super:

  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.

Uses:

  • Using super keyword we can explicitly point the immediate super class object and access all the super class members which are present with the same name of the subclass members from the sub class methods.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.   super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  Subdemo obj= new Subdemo();
  17.  obj.a=1;
  18. obj.b=2;
  19. obj.disply();

  20.  
  21. }
  22. }
  23. Output: 
  24. 1
  25. 2
  26. 10
  27. 20

  • Using super keyword we can explicitly call the super class constructor.
  • The statement which explicitly calling super class constructor should be always the first statement of the sub class constructor.
  • The statement which explicitly calls the super class constructor should be present inside a constructor only.


  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10.  SuperDemo(int x, int y){
  11.  a=x;
  12. b=y
  13. }
  14. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4.  
  5. SubDemo(int x, int y){
  6. super(10,20);
  7.  a=x;
  8. b=y
  9. }
  10.  
  11. void disply(){

  12. System.out.println(a);
  13. System.out.println(b);
  14.   super.show();
  15. }
  16.  
  17. public static void main (String args[]) {
  18.  Subdemo obj= new Subdemo(1,2);
  19.  
  20. obj.disply();

  21.  
  22. }
  23. }
  24. Output: 
  25. 1
  26. 2
  27. 10
  28. 20

 

Key points to remember:

  • It is a keyword used to store super class non static members reference in subclass object.
  • Pre defined instance variable used to hold super class object reference through sub class object.
  • Used to separate super class and sub class members if both have same name.
  • It must be used explicitly if super class and sub class members have the same name.
  • Used to call super class constructor from sub class constructor.


Factory Method Pattern


Factory Method Pattern:

  • Problem: Using new keyword we can not create object with flexibility and by applying restrictions.
  • Solution: Use Factory pattern (or) Factory method.
  • By defining a abstract class or an interface but let the subclass  decide which class object to instantiate.
  • A method of a class capable of constructing and returning its own class object or other class object is called "factory method".
  • There are two types of factory methods.
  1. Static factory method.
  2. Instance factory method.

 1.Static Factory method:

  • A static method defined to construct and return object of same class or different is known as static factory method.
  • Some of the pre defined static factory methods are as follows.
  1. Thread th= Thread.currentThread();
  2. Class c=Class.forName();
  3. Runtime rt=Runtime.getRuntime();
  4. Calendar c=Calendar.getInstance();

2.Instance Factory method:

  • A non static method defined to construct and return object of same class or different is known as instance factory method.
  • Some of the pre defined instance factory methods are as follows.
  1.  String s= new String("instance of");
     String s1=s.concat("java");
  2. StringBuffer sb=new StringBuffer("instance of");
    sb=sb.subString(0,2);
  3. Date d= new Date();
    String s=d.toString();
Program on Factory method / Factory Design pattern:

Step 1: create an interface:

  1. package com.instanceofjavaforus;
  2.  
  3. public interface Vehicle {
  4.  
  5.  public void engine();
  6.  
  7. }
     

 Step 2: create a Two Wheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class TwoWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("Two Wheeler");
  7.     }
  8.  
  9. }

Step 3:: create a Four Wheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class FourWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("FourWheeler");
  7.     }
  8.  
  9. }

Step 4: create a SixWheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class SixWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("SixWheeler");
  7.     }
  8.  
  9. }

Step 5: create a TestEngine class which is having factory method:

  1. package com.instanceofjavaforus;
  2.  public class TestEngine {
  3.  public Vehicle getVehicle(String venname){
  4.      if(venname==null){
  5.             return null;
  6.        }else if(venname.equalsIgnoreCase("TwoWheeler")){
  7.             return new TwoWheeler();
  8.         }else if(venname.equalsIgnoreCase("FourWheeler")){
  9.             return new FourWheeler();
  10.         }else if(venname.equalsIgnoreCase("SixWheeler")){
  11.             return new SixWheeler();
  12.         }
  13.     
  14.         return null;
  15.     }
  16. }

Step 6: create a FactoryDemo class:

  1. package com.instanceofjavaforus;
  2.  public class FactoryDemo {
  3.   public static void main(String args[]){
  4.  
  5.      TestEngine te= new TestEngine();
  6.  
  7.         Vehicle vehcle1=te.getVehicle("TwoWheeler");
  8.        vehcle1.engine();
  9.  
  10.         Vehicle vehcle2=te.getVehicle("FourWheeler");
  11.         vehcle2.engine();
  12.  
  13.         Vehicle vehcle3=te.getVehicle("SixWheeler");
  14.          vehcle3.engine();
  15.  }
  16. }

  1. OutPut:
  2. TwoWheeler
  3. FourWheeler
  4. SixWheeler

Design patterns in java


Design patterns:

  • Pattern means set of guide lines.
  • Design patterns are solutions to commonly reoccurring problems in software development.
  • Design patterns are well proven solutions to common software problems.
  • Design patterns are best practices to use software technologies effectively in application development.  
  • Design patterns used in analysis and requirement  phase of  SDLC.
  • Design patterns can be implemented by using programming language.

Advantages:

  • Reusable.
  • These are already defined solutions to common re occurring problems so it reduces time.
  • They are already defined so Easy to understand and debug.

Categorization:

  • These are categorized into two parts.
  1. Java SE Design patterns.
  2. Java EE Design patterns.

Java SE Design patterns: 

  •  In Java SE there are mainly three types.

1.Creational Design patterns:

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Singleton Pattern
  4. Prototype Pattern
  5. Builder Pattern.

2.Structural Design patterns:

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Flyweight Pattern
  7. Proxy Pattern

3.Behavioral Design patterns:

  1. Chain Of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Memento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Pattern
  11. Visitor Pattern
Read More:
Factory pattern


Sort ArrayList in descending order

Descending order:

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class SortArrayListDesc {
  7.  
  8.      public static void main(String[] args) {
  9.  
  10.             //create an ArrayList object
  11.             ArrayList arrayList = new ArrayList();
  12.  
  13.             //Add elements to Arraylist
  14.             arrayList.add(1);
  15.             arrayList.add(2);
  16.             arrayList.add(3);
  17.            arrayList.add(4);
  18.             arrayList.add(5);
  19.             arrayList.add(6);
  20.  
  21.             /*
  22.            Use static Comparator reverseOrder() method of Collections 
  23.         utility class to get comparator object
  24.            */
  25.  
  26.          Comparator comparator = Collections.reverseOrder();
  27.  
  28.           System.out.println("Before sorting  : "  + arrayList);
  29.         
  30.  
  31.            /*
  32.               use
  33.               static void sort(List list, Comparator com) method of Collections class.
  34.             */
  35.  
  36.             Collections.sort(arrayList,comparator);
  37.             System.out.println("After sorting  : + arrayList);
  38.  
  39.        }
  40.     }
     

  1. OutPut:
  2. Before sorting  : [1, 2, 3, 4, 5, 6]
  3. After sorting  : [6, 5, 4, 3, 2, 1]
     

Ascending order:


  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class SortArrayListAsc{
  7.  
  8.      public static void main(String[] args) {
  9.  
  10.             //create an ArrayList object
  11.             ArrayList arrayList = new ArrayList();
  12.  
  13.             //Add elements to Arraylist
  14.             arrayList.add(10);
  15.             arrayList.add(4);
  16.             arrayList.add(7);
  17.            arrayList.add(2);
  18.             arrayList.add(5);
  19.             arrayList.add(3);
  20.  
  21.           
  22.  
  23.           System.out.println("Before sorting  : "  + arrayList);
  24.         
  25.  
  26.            /*
  27.               use
  28.               static void sort(List list) method of Collections class.
  29.             */
  30.  
  31.             Collections.sort(arrayList);
  32.             System.out.println("After sorting  : + arrayList);
  33.  
  34.        }
  35.     }
     



  1. OutPut:
  2. Before sorting  : [10, 4, 7, 2, 5, 3]
  3. After sorting  : [2, 3, 4, 5, 7, 10]
Select Menu