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:

Anonymous said...

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

Unknown said...

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

Unknown said...

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

Hadoop Online trainings

jhonathan said...

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


Mobile Application Development and Testing Training

Unknown said...

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


SATHYA TECH said...

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

Karthika Shree said...

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

Sujitkumar said...

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

Nandhini said...

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

Unknown said...

Good presentation thanks for the helpful blog.
Java Online Training | Java Online Training in India | Java Online Training India | Java Online Training in Hyderabad | Java Online Training Hyderabad | Java Training in Hyderabad | Java Training in India | Java Training Institutes in India | Java Training Institutes in Hyderabad | Java Course in Hyderabad | Java Training | Learn Java Online | Online Java Training | Best Java online Training Institutes in Hyderabad | Best Java Training Institutes in Hyderabad | Best Institutes for Java | Java Institutes in Hyderabad | Best Institutes for Java in Hyderabad | Learn Java | Java Training Institutes in Hyderabad | Java Certification | Java Certification Training | Java Certification Training in Hyderabad | Java Certification Training in India

Melisa said...

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

Rajkamal said...

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

Anonymous said...

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

SAP Training in Chennai said...

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

SAP Academy said...

Beautiful explanation..
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP Basis Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai
SAP PM Training in Chennai
SAP PP Training in Chennai
SAP SD Training in Chennai

Aruna said...

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

Anonymous said...

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

vijay said...

I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Salesforce Training in Chennai

Salesforce Online Training in Chennai

Salesforce Training in Bangalore

Salesforce Training in Hyderabad

Salesforce training in ameerpet

Salesforce Training in Pune

Salesforce Online Training

Salesforce Training

jenani said...

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



vcube said...

I appreciate you sharing this content.
Angular js course in kphb