OOP Concepts

We are hosting this site in another domain. Checkout

http://www.techpitcher.com/java/oops.html


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

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 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 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 is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding.

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

2. 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
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.

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.

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.
----------------------------------------------------------------------------------
3. 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.)
----------------------------------------------------------------------------------
4. 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.”

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

1 comment:

Anonymous said...

Quality of content is good...try to add more questions