Java interview questions and answers on OOPS

Part 4

Java interview questions and answers on oops covered in this post:

    Java interview questions and answers on OOPS

        What is a constructor? What are the types of constructors?

    -          Constructor is a function that is used to initialize the object.

    -          Constructor doesn’t have a return type.

    -          Constructor name must be the same as a class name.

    -          We can have multiple constructors in one class.

    -          We can declare the constructor as private, public and protected.

    -          There are three types of constructors.

    1. Default Constructor: If you don’t provide any constructor compiler will provide you with, default constructor.

    2.   No-arg constructor: If you have declared a constructor without a parameter. That constructor is called the no-arg constructor.

    3.   Parameterized Constructor: If you declared a constructor with a parameter then it is called the parameterized constructor.

     

        Will the parent class constructor be invoked while you have invoked the child class constructor without a super keyword?

    -  Yes. If you create an object of child class and call child class constructor. Child class constructor internally calls parent class constructor implicitly. 

    package simplifiedjava.crackedInterview; 

    public class OverrideOverloadedMethodDemo { 

          public static void main(String[] args) {       

                Child c = new Child();             

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class Parent {     

          public Parent() {

                System.out.println("Parent Class Constructor");

          }    

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public Child() {

                System.out.println("Child Class Constructor...");

          }          

    } 

    output :

    Parent Class Constructor

    Child Class Constructor...


     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

        What is the difference between method and constructor?

     

    Method

    Constructor

    1.

    The method is used to implement the logic.

    Constructor is used to initializing the object.

    2.

    The method can have a return type.

    A constructor cannot have a return type.

    3.

    Method name can be anything or can be class name as well.

    The constructor name must be the same as the class name.

    4.

    The method must invoke explicitly.

    A constructor can invoke automatically.

    5.

    If you don’t have any method then the compiler won’t provide any kind of method like a constructor.

    If you don’t have any constructor then the compiler will provide a default constructor.

    6.

    The method can be overridden or overloaded.

    The constructor can be overloaded but cannot overridden.


        Can we have a constructor in abstract class? If we can't instantiate abstract class then why there is a need for a constructor in abstract class?

    -      Yes. We can declare a constructor inside the abstract class.

    -  Absolutely right we cannot instantiate abstract class but when its Implementation class or child class instantiate that time abstract class constructor will be called. 

     

        Can I declare the constructor as an abstract?

    -      Constructors cannot be abstract.

    -     Abstract keyword can be applied to class and method, not constructor or blocks.

     

        Can you declare constructor inside interface?

    -      Constructor cannot be declared inside interface.

    -    Constructor is mainly used to initialize the value. We cannot instantiate the interface so we cannot declare the constructor inside an interface.

     

        Can you declare constructor static, final, abstract and synchronized?

    -          We cannot declare constructor static, final, abstract and synchronized.

    -          Purpose of all four keywords given above is different.

     

        Why java doesn't allow static constructors?

    -       Static belongs to the class, not the object.

    -       Constructor belongs to object while initializing the value into it.

    -   When you create an object of child class then internally parent class constructor gets called automatically.

    -     If you declare the constructor as static then the child class cannot call the parent class constructor so it violates the inheritance mechanism. Hence we cannot declare the constructor as static.

     

        In your program, you have a static block, non-static block, constructor and main(). What will be the sequence of execution?

    -          The sequence of execution is as follows.

    1. Static Block: will be executed at the time of class loading into memory.

    2.  Non-Static Block: Will be executed at the time of instantiation. But make a note it depends if you are instantiating the class before printing any statement in main. If you instantiate the class after printing the statement.

    3.  Constructor: Will be executed at the time of object initializing.

    4.   Main: Will execute last if you haven’t instantiated the class.

    5.   We can see more scenarios.

    Scenarios 1:

    package simplifiedjava.crackedInterview; 

    public class BlockExecutionSequenceDemo { 

          static {

                System.out.println("Static Block Excecution");

          }    

          {

                System.out.println("Non Static Block Excecution");

          }           

          public BlockExecutionSequenceDemo() {

                System.out.println("Constructor Excecution");

          }     

          public static void main(String[] args) {       

                System.out.println("Main Method Execution");         

          }

    }

    Output:

    Static Block Excecution

    Main Method Execution

     

    Scenarios 2: 

    package simplifiedjava.crackedInterview; 

    public class BlockExecutionSequenceDemo { 

          static {

                System.out.println("Static Block Excecution");

          }    

          {

                System.out.println("Non Static Block Excecution");

          }           

          public BlockExecutionSequenceDemo() {

                System.out.println("Constructor Excecution");

          }     

          public static void main(String[] args) {

                BlockExecutionSequenceDemo demo = new BlockExecutionSequenceDemo();

                System.out.println("Main Method Execution");

          }

    }

    Output :

    Static Block Excecution

    Non Static Block Excecution

    Constructor Excecution

    Main Method Execution

     

    Scenarios 3:

    package simplifiedjava.crackedInterview; 

    public class BlockExecutionSequenceDemo { 

          static {

                System.out.println("Static Block Excecution");

          }    

          {

                System.out.println("Non Static Block Excecution");

          }           

          public BlockExecutionSequenceDemo() {

                System.out.println("Constructor Excecution");

          }

         

          public static void main(String[] args) {

                System.out.println("Main Method Execution");

                BlockExecutionSequenceDemo demo = new BlockExecutionSequenceDemo();

          }

    } 

    Output:

    Static Block Excecution

    Main Method Execution

    Non Static Block Excecution

    Constructor Excecution

     

        What is the purpose to make a constructor private?

    -    Private constructor ensures that only one object can be created at a time.

    -    Cannot instantiate the class from outside the class.

    -    Generally for singleton design pattern uses a private constructor.


    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here



    Upcoming questions in next post:

    41. What is constructor chaining?

    42. Can we override or overload the constructor?

    43. Can a constructor have a return type?  If no can you explain why?

    44. Can constructor have access specifiers? Which access specifiers are applicable to a constructor?

    45. Can constructor declare exception with throws keyword?

    46. What is the default constructor and what is the difference between the default constructor and no-arg constructor and parameterized Constructor?

    47. Can we declare multiple constructors in a single class?

    48. What is an abstract class? Can we instantiate abstract class?

    49. Can I declare a final and abstract method in abstract class?

    50. Can we declare a class as Abstract without having a single abstract method?



    Previous Post                                                                    Next Post



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     


    Post a Comment

    0 Comments