Tuesday, June 17, 2008

OOPS

What is OOPS? Answer: OOP is the common abbreviation for Object-Oriented Programming.

Describe the principles of OOPS.
Answer: There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.

Explain the Encapsulation principle.
Answer: Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

Explain the Inheritance principle.
Answer: Inheritance is the process by which one object acquires the properties of another object.

Explain the Polymorphism principle.
Answer: The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".

Explain the different forms of Polymorphism.
Answer: From a practical programming viewpoint, polymorphism exists in three distinct forms in Java: Method overloading Method overriding through inheritance Method overriding through the Java interface

Primitive values and type conversion
Assignment compatibility issues come into play for both primitive types and reference types. Values of type boolean can only be assigned to variables of type boolean (you cannot change the type of a boolean). Otherwise, a primitive value can be assigned to any variable of a type whose range is as wide or wider than the range of the type of the value. In that case, the type of the value is automatically converted to the type of the variable. (For example, types byte and short can be assigned to a variable of type int, because type int has a wider range than either type byte or type short.)

Conversion to narrower rangeOn the other hand, a primitive value of a given type cannot be assigned to a variable of a type with a narrower range than the type of the value, (unless the cast operator is used to force a type conversion). Oftentimes, such a conversion will result in the loss of data, and that loss is the responsibility of the programmer who performs the cast.

Assignment compatibility for references
Assignment compatibility, with respect to references, doesn't involve range issues, as is the case with primitives. Rather, the reference to an object instantiated from a given class can be assigned to:

* Any reference variable whose type is the same as the class from which the object was instantiated.

* Any reference variable whose type is a superclass of the class from which the object was instantiated. *

Any reference variable whose type is an interface that is implemented by the class from which the object was instantiated. *

Any reference variable whose type is an interface that is implemented by a superclass of the class from which the object was instantiated. Such an assignment does not require the use of a cast operator.
However, it is not possible to perform a successful cast to convert the type of a reference in all cases. Generally, a cast can only be performed among reference types that fall on the same ancestral line of the class hierarchy, or on an ancestral line of an interface hierarchy. For example, a reference cannot be successfully cast to the type of a sibling or a cousin in the class hierarchy.

Downcasting
When we cast a reference along the class hierarchy in a direction from the root class Object toward the leaves, we often refer to it as a downcast. While it is also possible to cast in the direction from the leaves to the root, this happens automatically, and the use of a cast operator is not required.

An abstract class may contain abstract methods or concrete methods, or a combination of the two. However, an abstract class cannot be instantiated. An interface also cannot be instantiated.)(Multiple inheritance is allowed with interfaces.

The cardinal rule in implementing interfaces is:
If a class implements an interface, it must provide a concrete definition for all the methods declared by that interface, and all the methods inherited by that interface. Otherwise, the class must be declared abstract and the definitions must be provided by a class that extends the abstract class. Important: When an interface method is invoked on one of the objects using the reference of the interface type, the behavior of the method will be as defined by the author of the specific class that implemented the interface. The behavior of the method will often be different for different objects instantiated from different classes that implement the same interface. The selection of a method for execution is based on the actual type of object whose reference is stored in a reference variable, and not on the type of the reference variable on which the method is invoked.

* the actual method called depends on the object being passed to the method
* Java uses late-binding to support polymorphism; which means the decision as to which of the many methods should be used is deferred until runtime
Polymorphism

The term polymorphism comes from the Greek words meaning "many shapes". In programming it refers to the difference in behavior depending on the actual type of an object.

For example:
Measurable x;
x = new BankAccount(1000);
x = new Coin(0.1,"dime");
Measurable is an interface, and BankAccount and Coin are classes which realize the interface. The object reference x can refer to an object of either BankAccount or Coin type. Measurable is not a class, it does not have a constructor, and no object of type Measurable can be constructed: x = new Measurable(); //ERROR
Methods declared in the interface can be called with x, even if we do not know its actual type: double d = x.getMeasure(); The actual method getMeasurable() that gets executed is resolved by the virtual machine depending on the class type of x. If x points to an object of class Coin, then the getMeasure() method of class Coin is called. Alternatively, if x refers to a BankAccount object, then the implementation of getMeasurable() in the BankAccount class is used. Therefore, the same method call can execute two different method implementations depending on the momentary contents of x. This behavior is called polymorphism, which is different than method overloading.

Difference between Polymorphism and Method Overloading
The difference between polymorphism and method overloading is in the time when the actual method to execute is determined. The reason for this is that when a method is overloaded, such as in: account = new BankAccount(); account = new BankAccount(1000);

The compiler can tell which constructor to use by the method signature, including the number and types of parameters provided. This selection of a method to use at compile time, before the program ever runs is called early binding. On the other hand, when we use a polymorphic method call such as x.getMeasure() the actual getMeasure() method called depends on what type of object x refers to. Because objects are not constructed until the program runs, the method called is determined at run-time. Therefore, the virtual machine, not the compiler selects the appropriate method. This method selection is called late-binding.



http://www.developer.com/java/other/article.php/1025601

No comments:

Topics