Wednesday, 22 August 2012

Introduction to OOPS Concepts



Class : is the real world entity .  It is the central point of OOP and that contains data and codes with behavior. In Java everything happens within class and it describes a set of objects with common behavior. The class definition describes all the properties, behavior, and identity of objects present within that class. 

Object : Object is an instance of a class . Objects are the basic unit of object orientation with behavior, identity. As we mentioned above, these are part of a class but are not the same. An object is expressed by the variable and methods within the objects. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods

Methods :  We know that a class can define both attributes and behaviors. Again attributes are defined by variables and behaviors are represented by methods. In other words, methods define the abilities of an object.

Inheritance : This is the mechanism of organizing and structuring software program. Though objects are distinguished from each other by some additional features but there are objects that share certain things common. In object oriented programming classes can inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding and reduces development time. 

Abstraction : The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). 

Encapsulation -: This helps in hiding an object's data describing its state from any further modification by external component.This is an important programming concept that assists in separating an object's state from its behavior. In Java there are four different terms used for hiding data constructs and these are public, private, protected and package. 

Polymorphism : Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

JIT:Just in Time Compiler 
When JVM compiles the class file he does not compile
the full class file in one shot. Compilation is done on function basis or file basis. Advantage gained from this is that heavy parsing of original source code is avoided. Depending on need basis the compilation is done. This typ of compilation is termed as JIT or Just-in-Time compilation.

Just-in-time compiler is a compiler used to convert the Commmon Intermediate Language (CIL) code into native code (also called machine code) that is processed by machine. 

A little description 
While compiling of .NET program, its code is converted into Common Intermediate Language code that is done by Common Language Runtime. 
But while executing the program this CIL code is converted into Machine code or Native code that is done by JIT Compiler.



Abstract Class :
An abstract class is a class that is declared by using the abstract keyword. It may or may not have abstract methods. Abstract classes cannot be instantiated, but they can be extended into sub-classes.
        --or--
Java provides a special type of class called an abstract class. Which helps us to organize our classes based on common methods. An abstract class lets you put the common method names in one abstract class without having to write the actual implementation code.

An abstract class can be extended into sub-classes, these sub-classes usually provide implementations for all of the abstract methods.

The key idea with an abstract class is useful when there is common functionality that's  like to implement in a superclass and some behavior is unique to specific classes. So you implement the superclass as an abstract class and define methods that have common subclasses. Then you implement each subclass by extending the abstract class and add the methods unique to the class.

Points of abstract class :

Abstract class contains abstract methods.
Program can't instantiate an abstract class.
Abstract classes contain mixture of non-abstract and abstract methods.
If any class contains abstract methods then it must implements all the abstract methods of the abstract class.




Access Modifiers in Java : 

All the four public, private, protected and no modifer is allowed.
  • public and no modifier – the same way as used in class level.
  • private – members CAN ONLY access.
  • protected – CAN be accessed from ‘same package’ and a subclass existing in any package can access.
For better understanding, member level access is formulated as a table:


Access Modifiers

Same ClassSame PackageSubclassOther packages
publicYYYY
protectedYYYN
no access modifierYYNN
privateYNNN
First row {public Y Y Y Y} should be interpreted as:
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same class’.
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same package’.
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘subclass’.
  • Y – A member declared as ‘public’ CAN be accessed from ‘Other packages’.
Second row {protected Y Y Y N} should be interpreted as:
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same class’.
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same package’.
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.
  • N – A member declared with ‘protected’ access modifier CANNOT be accessed by the members of the ‘Other package’.
similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.

1 comment: