Thursday, February 18, 2010

OOP Concept - Classes and Objects

1. Describe OOPs concepts.

The four main concepts are involved in OOP:
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Abstraction

Abstraction is hiding of information from others. In case of Java, its hiding implementation of a method/ object. For example if an interface is available to outer world, so end user won't know about implementing class details. This is just a kind of abstraction. Abstraction could be anywhere in java, wherever we hide some information, we call it abstraction.

Encapsulation

Encapsulation is combining data and method along with implementation hiding within the class. Implementation hiding is done with the help of access modifiers (public, protected, package, private).

Inheritance

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Although inheritance may sometimes imply (especially in Java, where the keyword for inheritance is extends) that you are going to add new methods to the interface, that's not necessarily true. The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method.

Polymorphism

Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding.

----------------------------------------------------------------------

2. What is an abstract class?

You create an abstract class when you want to manipulate a set of classes through this common interface. All derived-class methods that match the signature of the base-class declaration will be called using the dynamic binding mechanism.
A class containing abstract methods is called an abstract class. Class does not contain implementation for Abstract methods. If a class contains one or more abstract methods, the class itself must be qualified as abstract. (Otherwise, the compiler gives you an error message.)

----------------------------------------------------------------------

3. What is an interface?

The interface keyword takes the abstract concept one step further. You could think of it as a "pure" abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but these are implicitly static and final. An interface provides only a form, but no implementation. An interface says, "This is what all classes that implement this particular interface will look like."

----------------------------------------------------------------------

4. What is Polymorphism in java?

The meaning of Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding. In java polymorphism exists in three distinct forms

1. Method overloading

Method overloading can be done on the basis of number of argument in a method, or order of arguments or class of the function. Overloading can't be done on the basis of return type. This is because sometime methods are called only for their side effect and their return value might be ignored. E.g.

  public class Test {
        // First method
        public void display(){}
        // Second method
        public void display(String msg){}
        // Third method: Not allowed
        public String display(){}
  }
                                      

In the above example first and second display methods are overloaded on the basis of number of arguments while we can't define third display method with same arguments and different return type as compiler won't be able to differentiate that which method is called.

2. Method overriding through inheritance

Overloading methods in extended class which are defined in base class. E.g.

  public class ExtendedTest extends Test {
        // Overloaded Method
        public void display(String msg){}
        public static void main(String[] args) {
            ExtendedTest eTest = new ExtendedTest();
            eTest.display("ExtendedTest");
            Test test = new Test();
            test.display("Test");
        }
  }
                                      

That means the method "display" in the "ExtendedTest" class is given more preference than "display" in the "Test" class for any "ExtendedTest" object. This is what is called overriding the methods. If there is no "display" method defined in "ExtendedTest" class then object from "ExtendedTest" will use method defined in "Test" class. That means compiler doesn't have information at compile time that which method to call. This decision is takes at run time. That is why this is called late binding or runtime binding.

3. Method overriding through the Java interface

This could be the case when we have one interface implemented by more than one java class. Let's assume that there is an interface ITest and classes TestA & TestB both implement this interface. Any statement like -

  ITest test1 = new TestA();
  //Or
  ITest test2 = new TestB();
                                      

can't be resolved at compile time. This could be resolved at runtime.

----------------------------------------------------------------------

5. What is a static in java?

A class is the blueprint from which individual objects are created. We won't get anything until we create an object of that class with new and at that point we get space of data storage and handle (method) for this data.
There are two cases which don't fit in above situation. One we want only one storage of data for every object or even if no object is created. The second situation is that if we want a method that is not associated with any object. Method is directly associated with the class and operate on class data (static) only.
We can achieve both by static keyword. When we say variable or method static that means it is related with class and every instance will access same copy.
If a method is declared static then we can use only static data member inside it. Static method in super class can be overridden by static method in sub-class only. However we can't override a static method with non-static method. Static method is implicitly final.
It is not recommended to use instance name for accessing static data or method since this gives a feeling that data or method is associated with object and not with the class. But that is not the case.

----------------------------------------------------------------------

6. What are transient variables?

During serialization, there might be a particular variable that you don't want Java's serialization mechanism to automatically save and restore. This is commonly the case if that variable represents sensitive information that you don't want to serialize. Even if that information is private in the object, once it has been serialized, it's possible for someone to access it by reading a file or intercepting a network transmission.

To control this, you can turn off serialization on a field-by-field basis using the transient keyword. This keyword indicates that the value of this variable doesn't have to be serialized. When the object will be de-serialized, this variable will be initialized with a default value of its data type.

----------------------------------------------------------------------

No comments:

Post a Comment