Object Oriented Concepts in JAVA
OOPs concepts are the rules which are suppose to be satisfied by JAVA language in order to call that programming language as an object oriented programming language.
- Class
- Instance
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
1. Class:
Collection of objects. It is a legal entity.
2. Instance:
Any entity that has state and behaviour is known as object.
Ex: chair,pen,keyboard.
it may be physical or logical.
3. Abstraction:
Hiding internal details and showing functionality is known as Abstraction.
in java we use abstract classes and interface to achieve abstraction
4.Encapsulation:
The process of binding the data along with its related and corresponding methods is known as Encapsulation.
by this we are restricting the availability of the data to the unrelated methods.hence unrelated methods can't access the data directly. Class is the base for Encapsulation.
Ex: java bean is the fully encapsulated
5.Inheritance:
When one object acquires all the properties and behaviours of parent object i.e known as inheritance. It provides code re usability.
It is used to provide Runtime Polymorphism.
6. Polymorphism:
when one task is performed by different ways. Nothing but Defining multiple methods with the same name is known as Polymorphism.
Two types
1. Static Polymorphism\CompileTime Polymorphism
2. Dynamic Polymorphism\RunTime Polymorphism
1. Static Polymorphism:
The concept of defining multiple methods with the same name and with different parameters within the same calss.
2. Dynamic Polymorphism:
The concept of defining multiple methods with the same name and with same signature in super class and sub class .
Ex: to draw shape, it may be circle or rectangle etc
-------------------------------------------------------------------------------------------------------------------
1.CLASS:
It is a template or blueprint from which objects are created. A class is a group of objects that has common properties.
A class in Java contain:
-------------------------------------------------------------------------------------------------------------------
It is a template or blueprint from which objects are created. A class is a group of objects that has common properties.
A class in Java contain:
- Data member(static and instance)
- Method(static and instance)
- Constructor(default and with-arguments)
- block(static and instance)
- class and interface
- enum
- inherited data members
ex: class Structure
-------------------------------------------------------------------------------------------------------------------
2.OBJECT:
- Object is an instance of class
- it stores in heap memory
- for each class we can create more than one object.
- object has three characteristics:
state: represent the data(value) of object
behaviour: represents the functionality of the object
Identity: object identity is typically implemented via Unique ID. the value of the ID is not visible to the external user. But it is used internally by JVM to identify each object uniquely. - Number of ways to create object:5 ways
1. using 'new' keyword
2. using class.forName();
3. using clone()
4. using newInstance()
5. using object Deserialization()
1. using 'new' Keyword:
99% of the objects creates in this way.
ex: ClassA objectname=new ClassA();
2. using class.forName():
gives the class object , which is useful for reflection. The methods that this object has are defined by java, not by the programmer writing class. They are same for every class. Calling newInstance() on that gives you an instance for that class(i.e calling class.forName("ExampleClass").newInstance() .it is equivalent to calling new ExampleClass().on which you can call the methods that the calss define, access the visible fields etc.
class.forName(); will always use the class loader of the called, where as ClassLoader.loadClass() can specify a different ClassLoader.
3.using clone():
used to copy existing object.
Ex: ClassA a=new ClassA();
ClassA b=(ClassA)a.clone();
4.using newInstance():
ex:
Object ClassA a=ClassA.class.getClassLoader().loadClass("ClassA").newInstance()
5. using object Deserialization:
Object Deserialization is nothing but creating an object from its serialized form.
ex:
import java.io.*;
class ObjectCreation
{
public static void main(String[] args)throws ClassNotFoundException,InstantiationException,IllegalAccessException,CloneNotSupportedException,IOException
{
//using new keyword
ClassA obj=new ClassA();
//using class.forName()
Class C=Class.forName("ClassA");
ClassA obj2=(ClassA)C.newInstance();
//using clone
ClassA obj3=(ClassA)obj2.clone();
if(obj2!=obj3)
System.out.println("object copy created");
//using newInstance()
Object obj4 = ClassA.class.getClassLoader().loadClass("ClassA").newInstance();
//using Deserialization
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("abc.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeObject(obj4);
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("abc.txt"));
ClassA obj5 = (ClassA) ois.readObject();
}
}
class ClassA implements Cloneable,Serializable
{
ClassA()
{
System.out.println("Object created");
}
@Override
protected Object clone()throws CloneNotSupportedException
{
return (ClassA)super.clone();
}
}
O/P:
Object Created
Object Created
Object Copy Created
Object Created
ex:
import java.io.*;
class ObjectCreation
{
public static void main(String[] args)throws ClassNotFoundException,InstantiationException,IllegalAccessException,CloneNotSupportedException,IOException
{
//using new keyword
ClassA obj=new ClassA();
//using class.forName()
Class C=Class.forName("ClassA");
ClassA obj2=(ClassA)C.newInstance();
//using clone
ClassA obj3=(ClassA)obj2.clone();
if(obj2!=obj3)
System.out.println("object copy created");
//using newInstance()
Object obj4 = ClassA.class.getClassLoader().loadClass("ClassA").newInstance();
//using Deserialization
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("abc.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeObject(obj4);
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("abc.txt"));
ClassA obj5 = (ClassA) ois.readObject();
}
}
class ClassA implements Cloneable,Serializable
{
ClassA()
{
System.out.println("Object created");
}
@Override
protected Object clone()throws CloneNotSupportedException
{
return (ClassA)super.clone();
}
}
O/P:
Object Created
Object Created
Object Copy Created
Object Created
---------------------------------------------------------------------------------------------------------------------
3.ABSTRACTION:
i. Abstract Class/method:
abstract classname{}
abstract classname{
abstract returntype methodname(arguments);
}
ii.interface:
interface interfaceName{
}
----------------------------------------------------------------------------------------------------------------
4. ENCAPSULATION:
3.ABSTRACTION:
i. Abstract Class/method:
abstract classname{}
abstract classname{
abstract returntype methodname(arguments);
}
ii.interface:
interface interfaceName{
}
----------------------------------------------------------------------------------------------------------------
4. ENCAPSULATION:
Comments
Post a Comment