Friday, 29 September 2017

Object Oriented Programming : Class and Object

1.Object Oriented Programming

Since Java is an object oriented language, complete java language is build on classes and object. Java is also known as a strong Object oriented programming language(oops).

OOPS is a programming approach which provides solution to problems with the help of algorithms based on real world. It uses real world approach to solve a problem. So object oriented technique offers better and easy way to write program then procedural programming model such as C, ALGOL, PASCAL.

One of the eight classes of java.lang package are known as wrapper class in java. The list of eight wrapper classes are given below.

Main Features of OOPS

1.Class

2.Object.

3.Inheritence

4.Polymorphism

5.Encapsulation

6.Abstraction

7.Instance

8.Method

9.Message

Now Let see one by one All the Basic Building Blocks of  object oriented Programming

1.Class

In Java everything is encapsulated under classes. Class is the core of Java language. Class can be defined as a template/ blueprint that describe the behaviors /states of a particular entity. A class defines new data type. Once defined this new type can be used to create object of that type.

A class is declared using class keyword. A class contain both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

Object is the physical as well as logical entity whereas class is the logical entity only.

A class in java can contain:

data member

method

constructor

block.

class and interface 

 

Syntax:

class < class_name > {
        data member;
        method;
}

Rules for Java Class:

A class can have only public or default(no modifier) access specifier.

It can be either abstract, final or concrete (normal class).

It must have the class keyword, and class must be followed by a legal identifier.

It may optionally extend one parent class. By default, it will extend java.lang.Object.

It may optionally implement any number of comma-separated interfaces.

The class's variables and methods are declared within a set of curly braces {}.

Each .java source file may contain only one public class. A source file may contain any number of default visible classes.

Finally, the source file name must match the public class name and it must have a .java suffix.

A simple class example:

Suppose, Student is a class and student's name, roll number, age will be its property. Lets see this in Java syntax.

class Student.
{
String name;
int rollno;
int age;
}
When a reference is made to a particular student with its property then it becomes an object, physical existence of Student class.

2.Object

An entity that has state and behavior is known as an object. Object is an instance of class. You may also call it as physical existence of a logical template class.

An object has three characteristics:

state : represents data (value) of an object.

behavior : represents the behavior (functionality) of an object such as deposit, withdraw etc.

identity : Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

There are three steps when creating an object from a class:

Declaration : A variable declaration with a variable name with an object type.

Instantiation : The 'new' keyword is used to create the object.

Initialization : The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Student std=new Student(); //here std is object of class Student

After the above statement std is instance/object of Student class. Here the new keyword creates an actual physical copy of the object and assign it to the std variable. It will have physical existence and get memory in heap area. The new operator dynamically allocates memory for an object.

Q. Difference between object and class ???

Object Class
Object is an instance of a class. Class is a blueprint or template from which objects are created.
Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.
Object is a physical entity. Class is a logical entity.
Object is created through new keyword mainly e.g.
Student s1=new Student(); Class is declared using class keyword e.g.
Object is created many times as per requirement. Class is declared once.
Object allocates memory when it is created. Class doesn't allocated memory when it is created.
There are many ways to create object in java such as new keyword, newInstance() method, clone() method, factory method and deserialization. There is only one way to define class in java using class keyword.

Q. What are the different ways to create an object in Java ??

There are many ways to create an object in java. They are:

By new keyword.

By newInstance() method.

By clone() method.

By factory method etc.

3.Methods in Java

Method describe behavior of an object. A method is a collection of statements that are group together to perform an operation.

Syntax:

modifier return-type methodName ( parameter-list )
{
       //body of method
}

Example of a Method

public String getName(String st)
{
String name="betheDeveloper.com";
name=name+st;
return name;
}

Modifier : Modifier are access type of method and its optional to use.

Return Type : A method may return value.

Method name : Actual name of the method.

Parameter : Value passed to a method.

Method body : Collection of statement that defines what method does.

Parameter Vs. Argument:

Parameter is variable defined by a method that receives value when the method is called. Parameter are always local to the method they dont have scope outside the method. While argument is a value that is passed to a method when it is called.

The finalize( ) Method:

It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.

The finalize( ) method has this general form:

protected void finalize( )
{
   // finalization code here
}

Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.





No comments:

Post a Comment

Abstract Class & Interface in Java

      Abstract Class & Interface in Java         Here is the most important topic on Java Abstract class and interface, Abstraction ...