Java interview questions and answers on oops

 Part 1

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


    Java interview questions and answers on oops

        What are the main pillars of OOPs Concepts? Explain the OOPS Concepts?

    -          There are four pillars of OOPs Concepts which are as follows.

    1.       Abstraction:

    Abstraction means hiding the implementation. 

    Real-Life Example: We can take an example of a Car. When we want to start a car we have to just turn keys in the ignition and the engine gets started. For the end-user, there is no need for how the engine is interconnected with different wheels. How the battery supply power. How petrol sprays in the engine. These all implementation is hidden. This is an abstraction. 

    Technical Implementation: Abstract class and Interface implements the abstraction. An abstract class is partial abstraction because abstract class can have concrete methods as well as abstract methods and the interface is full abstraction because the interface contained only abstract methods, not concrete methods.


    2.       Encapsulation:

    Encapsulation means hiding the data. 

    Real Life Example: Capsule is the perfect example of encapsulation. The capsule has different types of small tablets inside a wrapper. 

    Technical Implementation: Bean class is an example of Encapsulation. We declare properties as private and provide public getter and setter methods for each variable.

     

    3.       Inheritance:

    Inheritance means one object can access the properties of other objects. The child class can inherit the properties of the parent class. 

    Real-Life Example: Say suppose your grandfather has agricultural land, farmhouse and your father has flat, some gold and shares and you have racer bike, iPhone and laptop. Ultimately whatever property your grandfathers have will be inherited to your dad and your dad’s property inherited to you. But remember reverse is not possible. 

    Technical Implementation: You can implement inheritance using extends and implementation keywords. If you are extending another class then you have to use extends keyword and If you implement the interface then you have to use the implement keyword.

     

    4.       Polymorphism:

    Polymorphism means one name different form.

    An entity can have different behaviour in different situations. 

    Real-Life Example: A person can have a different role at a time with a different location or context. The person can be a father in the home, the person can be a manager in the company, Person can be a dancer in the movie. 

    Technical Implementation: Polymorphism can be implemented by using Method overloading and method overriding.


        What is the difference between compile-time polymorphism and run-time polymorphism?

     

    Compile time Polymorphism

    Run time polymorphism

    1.

    Method overloading is an example of Compile-time binding.

    Method overriding is an example of Run-time binding.

    2.

    Compile-time polymorphism can be achieved by static binding.

    Run-time polymorphism can be achieved by dynamic binding.

    3.

    Inheritance is not mandatory to implement compile-time polymorphism.

    Inheritance is mandatory to implement Run- time polymorphism.



     

    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


        How have you implemented Encapsulation in the Project?

    -      You can create POJO(Plain Old Java Object) class.

    -     You have to declare all members with private access modifier and provide public getters and setters method. So the original value of that variable cannot be change cause they are private and outsiders can use it cause we have provided public getters and setters methods.

    package simplifiedjava.crackedInterview; 

    public class EncapsulationImplementation { 

          private int id;

          private String name;     

          public int getId() {

                return id;

          }

          public void setId(int id) {

                this.id = id;

          }

          public String getName() {

                return name;

          }

          public void setName(String name) {

                this.name = name;

          }    

    }

     

        Can you explain the types of Inheritance supports in java?

    -          Java Supports 3 types of Inheritance.

    1.       Single Inheritance:

    In Single Inheritance, only one class can extend at a time.

    In single inheritance, there is only one child class and parent class.

    Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class Human { 

    }

    public class Men extends Human{     

    }

     

    2.       Multilevel Inheritance:

    When one class extends another class and that another class extends a subsequent class this type of inheritance is called Multilevel Inheritance.

    Every child class becomes a parent class of its subsequent child class.

    Please refer to an example below.

    package simplifiedjava.crackedInterview; 

    public class Human {

    }

    public class Men extends Human{     

    } 

    public class Boy extends Men{           

    }

     

    3.       Hierarchical Inheritance:

    When two or more classes extends one class is called Hierarchical Inheritance.

    Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class Human { 

    }

    public class Men extends Human{     

    }

    public class Women extends Human{           

    }

     

        Why java doesn't support Multiple Inheritance?

    -    To avoid an ambiguity situation java doesn’t support multiple inheritance.

    -   We can illustrate with an example. You have three classes. Class A, Class B and Class C.

    Class C extends Class A and Class B. Say suppose Class A and Class B has printHello() method. The compiler cannot decide whether which class printHello() method should inherit in class C. So avoiding this ambiguity situation java doesn’t support multiple inheritance.

    -     But indirectly java supports multiple inheritance through interfaces.

     

        How would you implement Multiple Inheritance in java?

    -     One way you can implement multiple interfaces.

    -   The second one is, one interface extends multiple interfaces and class implements the first interface. 


        Can you explain the public static void main(String[] args) Method?

    -          Main method is a starting point of java program execution.

    -          Main method components:

    1.       public: Access modifier of main() is public so the method can be invoked from outside. This method must be public so the java runtime can invoke it. If you make the main method private or protected then you won’t get a compile-time error but a runtime exception.

    2.       static: We are aware of the use of static. Without creating an object we can invoke the method of the class. So java runtime can invoke the main method without instantiating the class.

    3.       void: return type of the main method is void cause the main method doesn’t return anything.

    4.       main: This is the fixed name of the main method. You cannot change the name of the main method otherwise you will get a runtime exception.

    5.       (String args[]) : These are the command line argument. We can pass these arguments to the java program.

     

        List out which is wrong main method declaration.

    -          public static void main(String[] args) {}           Valid

    -          public static void main(String args) {}           Valid

    -          public final static void main(String args) {}      Invalid

    -          public static void main(String args) {}           Invalid

    -          private static void main(String[] args) {}         Invalid    

    -          protected static void main(String[] args) {}       Invalid

     

        Why main method is static and public?

    -          Main () method is the entry point of program execution. JVM calls the main method internally.

    -          If the method is declared as static then there is no need to create an object to call that method. JVM does not need to create an object to call the main() method.

    -          If the method is declared as public then that method is accessible outside the class and package.

     

        Can we overload or override the main method?

    -          We can overload the main() method but we can’t override the main() method. The main () method can’t override because the static method can’t be overridden. The static method belongs to the class, not the object.

    package simplifiedjava.crackedInterview; 

    public class Parent { 

          public static void main(String[] args) {

                // TODO Auto-generated method stub

          } 

          public static void main(String[] args, int num) {

                // TODO Auto-generated method stub

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public static void main(String[] args) {

                // TODO Auto-generated method stub

          }

    }


    • 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:

    11. Can we declare main method as a final. if not then why not?

    12. Can we create method with same name as class name with return type. Will the code compile. Put example will be clear.

    13. Can we override the overloaded method.

    14. Can we execute a program without main method.

    15. What is the purpose of static method and static variable.

    16. What is the difference between instance method and static method.

    17. What is the difference between static block and instance block.

    18. Can we access non-static member inside static area either method or block.

    19. What is method overriding and method overloading.

    20.Will the code compile If parent class method is not throwing any exception but child class method is throwing an checked exception.



    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