OBJECT ORIENTED PROGRAMMING

CONCEPTS OF OBJECT ORIENTED PROGRAMMING

 

1.  DATA ABSTRACTION

Abstraction is an act of representing only essential features without including the background details. For e.g. you are driving a car , you only need to know how to drive it and you don't care about its internal working how car is made (wiring , motor working). Similarly you need to know only url to access our website not how it is travelling from our servers to your browser.

 

2. DATA ENCAPSULATION

It is the way of combining both data and the methods that operate on data under a single unit, just like a capsule having similar components inside

 

3.  INHERITANCE

Inheritance is the capablitiy of one class of things to inherit the characteristics from another class.

 

4. POLYMORPHISM :

Polymorphism is the ability for a message or data to be processed in more than one form.

 

 

TWO COMPONENTS IN OOPS

CLASS

  •  A class is a blueprint or template from which objects are made  OR  a class is a way of binding the data describing an entity and its associated functions together.
  •  We use keyword "class" to declare a class.
  • class   name-of-class


           {        // body of class       }

OBJECT

 Object is an entity that has state and behavior.

 

CLASS CONSIST OF TWO PARTS

  • INSTANCE FIELDS :  data in an object.
  • METHODS : functions associated with objects to perform tasks.

 

FIELDS are of three types : - 

  • Variables
  • Constants
  • Static fields

 

1. VARIABLES

1.1 Local variables  => These are declared within a method and cannot be accessed outside the method.There scope is in method only.

1.2 Instance variables    => These are the variables declared inside the class but outside the method.

1.3 Reference variables   => These variables used to refer to an instance of class.These are just like pointers in C++ i.e. they point to the object location in memory.

 

2. CONSTANTS

constants are declared with the keyword "final".

for eg : final double pi = 3.14152967

 

3. STATIC FIELDS

  • If you define a field as static then there is only one such field per class.
  • static field is shared among all instances of objects.
  • If you change the value of static field in one object, then all objects automatically changes to their values to that new value.
  • static field belong to the class, not to any particular object.

 

METHODS in java can be divided into two categories

  •  General Methods
  •  Static Methods

1.1 CONCRETE METHODS   =>  normal methods that have parameters , return type and definition to perform a specific task.

 

1.2 ABSTRACT METHODS   =>  methods that do not have definition only declaration. eg:   public void sum() {   }

 

2. STATIC METHODS   

  •  Methods that do not operate on objects.You can call static methods without having any objects.
  • You cannot access instance fields from static objects.
  • It access only static fields of the class.

 

WHAT IS CONSTRUCTOR ?

 Constructors are special methods having some features :-

1. Same name as class name

2. No return type

3. Used to initialize fields of an object

4. Can be overloaded

5. We can have private constructor

6. If no constructor is created then default constructor is created automatically but if we create any parameterized constructor then default constructor is not created automatically


7. A constructor is always called with the new keyword.

8. You cannot apply a constructor to an existing object to reset the instance fields.

 

Types of Constructors

There are two types of constructors :

DEFAULT CONSTRUCTOR  

  • A constructor with no parameters.
  • If you write a class with no constructors what-so-ever, then default constructor is provided for you.
  • This default constructor sets all the instance fields to their default values  i.e. numeric data is set to 0 , boolean values are set to false , all object variables would be set to null.

 

public Empolyee()

{

      name = "" ;

      salary = 0;

      hireDay = new Date();

}

 

2. PARAMETRIZED CONSTRUCTOR

  • A constructor with parameters.
  • If a class supplies atleast one constructor but does not supply default constructor , it is illegal to construct objects without construction parameters.

 

public Employee ( String n , int m )

{

name = n;

salary = m;

}

OBJECTS CREATION

 

OBJECT VARIABLE

The object variable is used to refer to an object i.e. value of any object variable is a reference to an object that is stored somewhere else.

OBJECT

Object is constructed when you combine a constructor with the new operator.

NEW OPERATOR

It is used to allocate the memory to an object at the runtime.

 

 


  • The expression new Date() makes an object of type Date and its value is a reference to that newly created object.
  • That reference is then stored in the birthday variable.
  • An object variable can be explicitly set to null which indicates that it currently refers no object.

                   birthday = null ;

NOTE :

  • Runtime Error occurs if a method is applied to a variable that holds null.
  • Variables are not initialized to null automatically, they have to be initialized either by calling new or by setting them to null.


PROGRAM

read comments for better understanding



 

WHAT IS PACKAGE ?

-> A folder having related set of classes and interfaces. Packages can be of two types
1. Java API

a. java.lang
b. java.util
c. java.awt
d. java.rmi
e. javax.swing

f. javax.servlet
g. javax.servlet.jsp

2. Custom Packages

Creating a package using Notepad

- First decide the folder to hold your packages

- Now decide your packages names

o d:\javajust

o first

o second

- Now create sub folders with those names in main folder

- Now create the java classes place into these package

- Every such class must have package command on top.

 

 

Using the package from Command Prompt

To use the packages first set the CLASSPATH to lookup the packages on the disc

SET CLASSPATH=%CLASSPATH%;d:\javajust;

 

Using the package in NetBeans

- Open your project and add your package folder into your libraries

 

Distributing Packages

- We can distribute the packages in two forms

- To create a .zip file using winzip tool

o As .zip file

o As .jar file

o Add the .zip file name into classpath