Pages

Thursday, July 14, 2011

Object Oriented Programming - Concepts

Object Oriented Programming

OOP is Nothing but Object Oriented Programming.
In OOPs concept is implimented in our real life systems.
OOPs have following features
1. Object - Instance of class
2. Class - Blue print of Object
3. encapsulation - Protecting our data
4. polymorphism - Different behaviors at diff. instances
5. abstraction - Hidding our irrelavance data
6. inheritence - one property of object is aquring to
another property of object

Simple Ex.
Please assume u standing near a car.
How to impliments our OOPs concept for this scenario ?
Simple,
car is a object b'coz it having more functions.
car is a class b'coz it contain more parts and features inside.
car is a Encapsulation B'coz it protected some unwanted parts or
functions to user car is a Polymorphism b'coz it have different
speed as display in same speedometer car is a Abstraction b'coz
it hidding more parts by coverig such as engine,disel tank car
is a Inheritance b'coz one car is a property of more people.
i mean your car is driving bu you, your friend and your relatives.

Wonderful source link for OOPS concepts:-
http://www.desy.de/gna/html/cc/Tutorial/tutorial.html


Q:what is difference between instance and object.?
instance means just creating a reference(copy) .
object :means when memory location is associated with the object( is a runtime entity of the class) by using the new operator

Q:what are the all difference between interface and abstract class?
interface is a set of abstract methods, all of which have to be overriden by the class whichever implements the interface
abstract class is a collection of data and methods which are abstact (not all of them)

Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.

In short Interface is a abstract class having all methods abstract.
Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.
If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.

If you use a virtual keyword in a method then you can override this method in the subclass if you wish..
If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.


1) What is meant by Object Oriented Programming?
OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

2) What is a Class?
Class is a template for a set of objects that share a common structure and a common behavior.

3) What is an Object?
Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a class.

4) What is an Instance?
An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.

5) What are the core OOP’s concepts?
Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts.

6) What is meant by abstraction?
Abstraction defines the essential characteristics of an object that distinguish it from all other kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the perspective of the viewer. Its the process of focussing on the essential characteristics of an object. Abstraction is one of the fundamental elements of the object model.

7) What is meant by Encapsulation?
Encapsulation is the process of compartmentalizing the elements of an abstraction that defines the structure and behavior. Encapsulation helps to separate the contractual interface of an abstraction and implementation.

What is meant by Inheritance?
Inheritance is a relationship among classes, wherein one class shares the structure or behavior defined in another class. This is called Single Inheritance. If a class shares the structure or behavior from multiple classes, then it is called Multiple Inheritance. Inheritance defines “is-a” hierarchy among classes in which one subclass inherits from one or more generalized superclasses.
9) What is meant by Polymorphism?
Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class.
10) What is an Abstract Class?
Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behavior, typically by implementing its abstract operations.

11) What is an Interface?
Interface is an outside view of a class or object which emphasizes its abstraction while hiding its structure and secrets of its behavior.

12) What is a base class?
Base class is the most generalized class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

13) What is a subclass?
Subclass is a class that inherits from one or more classes
14) What is a superclass?
superclass is a class from which another class inherits.

15) What is a constructor?
Constructor is an operation that creates an object and/or initializes its state.

16) What is a destructor?
Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM.
17) What is meant by Binding?
Binding denotes association of a name with a class.

18) What is meant by static binding?
Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.

19) What is meant by Dynamic binding?
Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.

20) Define Modularity?
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

21) What is meant by Persistence?
Persistence is the property of an object by which its existence transcends space and time.

22) What is collaboration?
Collaboration is a process whereby several objects cooperate to provide some higher level behavior.

23) In Java, How to make an object completely encapsulated?
All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables.

24) How is polymorphism achieved in java?
Inheritance, Overloading and Overriding are used to achieve Polymorphism in java.

25) What is sealed and abstract class?

Source link:- http://msdn.microsoft.com/en-us/library/ms173150%28v=vs.80%29.aspx

Compile time polymorphism is functions and operators overloading.
Runtime time polymorphism is done using inheritance and virtual functions.
eg compile time polymorphism -- method overloding

run time time polymorphism -- method overriding

Static classes and static methods

  1. Static Class occupy memory during compile time.
  1. Static class cannot be instantiated. It calls its data members and member functions itself.
  1. Static class can have static members only as its cannot declare instance members in a static class
  1. Static class constructor get called whenever any static member is called.
  1. General Class can contains static methods but they too called its-self by class name.

No comments:

Post a Comment