Pages

Friday, 6 September 2013

Core Java - OOPS concepts (FAQ's)








1.    What is abstraction ?

Abstraction is nothing but showing the set of services by hiding internal implementation details.
i.e. highlight services only which matters to client, don't show how they are achieved.
Example:

Abstraction can be implemented by using abstract classes & interfaces.

Advantages:

  • Security - Services are provided but not internal implementation.
  • Easy Enhancement - We can change internal implementation without effecting outside person




2.    what is Encapsulation ?

Grouping methods & corresponding data into a single capsule is called encapsulation.

Hiding data behind the methods is also called as encapsulation.

Any component which follows data hiding & abstraction is also called encapsulation.
(Data hiding is nothing but hiding data from outside person, so that it cannot be directly accessible. This is achieved by special modifier called "private").

Pros:

  • Maintainability & modularity will be improved.
  • Security
  • Modifications become very easy.

 Cons:

  • Increases the code length
  • Slow down the execution of program.

Note:
If all variables are declared private in a class then it is called "Tightly Encapsulated class".
Example:
Class TightlyEncapsulatedClass
  {
     private int i = 10;
     private char c = 'a';
     private flaoat f = 10.5f;
  }

Class NotTightlyEncapsulatedClass
  {
     private int i = 10;
     private char c = 'a';
     public float f = 10.5f;
  }




3.    What is inheritance ?

When we have Is-A-Relationship between two classes then it is called inheritance & it is achieved by using EXTENDS keyword. 

Benefit:
Code re-usability.

Example:

Class Parent
   {
        method_m1();
        method_m2();
   }

Class Child extends Parent
   {
        method_m3();
        method_m4();
   }

Class Testing_Inheritance
  { 
        Parent P = new Parent();      <==  Parent class object created
        P.method_m1();                <==  Access parent class method using 
                                           parent object reference.  (VALID)
        P.method_m2();                <==  Access parent class method using 
                                           parent object reference.  (VALID)
        Child C = new Child();        <==  Child class object created
        C.method_m1();                <==  Access parent class method using 
                                           child object reference.   (VALID)
        C.method_m2();                <==  Access parent class method using 
                                           child object reference.   (VALID)
        C.method_m3();                <==  Access child class method using 
                                           child object reference.   (VALID)
        C.method_m4();                <==  Access child class method using 
                                           child object reference.   (VALID)

        Parent P = new child();       <==  Parent class object created
        P.method_m1();                <==  Access parent class method using 
                                           parent object reference.  (VALID)
        P.method_m2();                <==  Access parent class method using 
                                           parent object reference.  (VALID)
        P.method_m3();                <==  Compilation Error, 
                                           Parent reference cannot call 
                                           child specific methods (INVALID)
        P.method_m4();                <==  Compilation Error,  
                                           Parent reference cannot call 
                                           child specific methods (INVALID)

        Child c = new Parent();       <==  Compilation Error, 
                                           child reference cannot hold parent
                                           class object (INVALID)


Note:

  • In Java multiple inheritance is not allowed but multi level single inheritance is allowed. This is just to reduce the complexities from java program which we have in C++.
  • Multiple inheritance can be achieved in java using interfaces.
  • Class cannot access more than one class at a time but interface can implement any number of interfaces.




4.    What is Polymorphism ?

Here poly means "Many" and morpism means "forms".
So, one name with many forms is nothing but Polymorphism.

Types of Polymorphism

Static Polymorphism

  • Overloading
  • Method Hiding

Dynamic Polymorphism

  • Overriding


Overloading:
Two methods with same name and different arguments are called overloaded methods.
Two constructors of any class with different arguments are called overloaded constructors.

So, the process of defining same method or constructor with different arguments is called overloading.
In overloaded methods, compiler takes care of identifying which method is called based on reference type, hence it is also termed as static polymorphism or Early binding or compile time polymorphism.

Example:

Class TestOverloading
         {
             mthod_m1 (int arg1, char arg2)      <== Method
                  {
                  }
             mthod_m1 (int arg1, float arg3)     <== Overloaded method
                  {
                  }
             mthod_m1 (int arg1)                 <== Overloaded method
                  {
                  }

             TestOverloading()                   <== Constructor
                  {
                  }
             TestOverloading(int arg1)           <== Overloaded Constructor   
                  {
                  }
             TestOverloading(String s)           <== Overloaded Constructor
                  {
                  }
         }



Overridding:

Here consider we have 2 classes, (parent & child classes)

Class Parent 
   {
       method_m1 (int i)               Overridden method
          {
              -----service-A
              -----Service-A
          }
   }


Class Child extends Parent 
   {
       method_m1 (int i)               Overriding method
          { 
              -----service-A
              -----Service-A
          }
   }

Child class always can use parent class methods, but in some cases we need to change the implementation of parent method. This can be done in child class 7 is called Overriding.

Process of redefining parent class method in child class is called as Overriding.
In Overriding resolution is always taken care by JVM, based on run time object. thats the reasong overriding is also called as Dynamic polymorphism or Runtime polymorphism or LateBinding.


Method Hiding:

Method hiding is almost like overloading except few difference which are:
If both parent and child classes method are static, then we won't get any compile time error. It looks like overriding but it is not, this is method hiding.
Method hiding is also called as static polymorphism or early binding or compile time binding.




5.    What are constructors ?

Whenever we create any object, automatically some piece of code is automatically executed to perform initialization. This piece of code is nothing but a constructor.

Main purpose of constructor is to perform initialization of object.

Example:

Class TestDefaultConstructor
     {
         int i = 10;
         float f = 10.5f;
      }

After Compilation; code will look like -

Class TestDefaultConstructor
     {
         int i = 10;
         float f = 10.5f;
         TestDefaultConstructor()      <== Default constructor added by compiler
           {
           }
      }

Note:

  • Compiler adds Default constructor only if programmer doesn't specify any constructor in class
  • If programmer also code constructor, compiler doesn't append default constructor.


Constructor is called during object creation:
Example:

Class Test
    {
       TestDefaultConstructor T = new TestDefaultConstructor();
                   <== Default constructor will be called during object creation
       System.out.println("i=" + T.i);
       System.out.println("f=" + T.f);
    }

Note:

  • whenever object  is created without any argument then default constructor will be called.
  • Whenever user want to define any constructor, it is recommended to define the constructor without any argument in addition to parameterized constructor if declared. This way user can avoid any compilation error during object creation, if object is created without any argument/parameter.





More Java Interview questions:

Java.lang Package      |       Exception handling      |       Access Modifiers      |       Interfaces








20 comments:

  1. Is advanced java other than core concepts required for hadoop in real time ????

    ReplyDelete
  2. Great blog with an important content. The Author has done a great job. The Blog contain Core Java - OOPs Concepts(FAQ's), this information is really helpful for java learners are in the biggining stage. keep sharing such a wonderful information. Suppose if your looking for java Interview Q/A's just have a look: Java Interview Questions

    ReplyDelete
  3. java core concepts are very useful for beginners and advanced learners keep on sharing such valuable information

    Hadoop Online trainings

    ReplyDelete
  4. This is really nice. Thanks for sharing this informative article.


    Mobile Application Development and Testing Training

    ReplyDelete
  5. Hi sir really this important information is more useful for my search job on best java jobs in hyderabad


    ReplyDelete
  6. thank you for the content.we are very greatful to your blog such a nice information.one of the recommanded blog for students and professionals
    keep it up.we are more Knowledge from
    best regards

    Sathya technologies

    ReplyDelete
  7. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    ReplyDelete
  8. Nice blog. Really helpful information about Java …. Please keep update some more…………

    ReplyDelete
  9. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Android Training in Bangalore

    ReplyDelete
  10. Java is awesome. I believe I will still be able to use it even when I am 60 years of age. Java is a beautiful language. I also used it to develop my research project. J2EE Training in Chennai | JAVA Training in Chennai

    ReplyDelete
  11. TIB Academy is one of the best corejava Training Institute in Bangalore. We Offers Hands-On Training with Live project.

    ReplyDelete
  12. thanks for sharing the information.Indian Cyber Army is announcing “ Summer Internship 2018” on “ Ethical hacking and Cyber Crime Investigation” for the enthusiasts of Cyber security. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization

    ReplyDelete
  13. Very Good Blog. Highly valuable information have been shared. Highly useful blog..Great information has been shared. We expect many more blogs from the author. Special thanks for sharing..
    SAP Training in Chennai | AWS Training in Chennai | Android Training in Chennai | Selenium Training in Chennai | Networking Training in Chennai

    ReplyDelete
  14. Excellent Information's are shared...The persistence of This Blogs about JAVA give's a successful Result in feature...keep sharing
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  15. MP Board 12th Class Blueprint 2021 English Medium & Hindi Medium PDF download, MPBSE 12th Blueprint 2021 Pdf Download, mpbse.nic.in 12th Blue Print, Marking Scheme and Arts, Commerce and Science Streams Chapter wise Weightage pdf download. MP Board 12th Blue Print || MPBSE 12th Model Papers || MPBSE 10th Model Papers

    Manabadi AP Intermediate 2nd Year Model Question Paper 2021 MPC, BIPC, CEC, MEC group TM, EM Subject wise Blue Print, Download BIEAP Intermediate Second Year Model Question Papers, AP Senior Inter Test Papers, Chapter wise important Questions download. || AP Inter MPC, Bi.PC, CEC Blue Print || AP Inter 1st / 2nd Year Model Papers || AP 2nd year inter Test Papers

    Kar 1st / 2nd PUC Blue Print

    ReplyDelete
  16. Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in OMR

    Java Training in Annanagar



    ReplyDelete

Please enter comments related to post only..!! Thank You..!!