Object:
Object is the physical as well as logical entity where as class is the only logical entity.Class: Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.
A class in java contains:
- Data Member
- Method
- Constructor
- Block
- Class and Interface
An Object in java has three characteristics:
- State
- Behavior
- Identity
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.
Class is also can be used to achieve user defined data types.
In real world many examples of object and class like dog, cat, and cow are belong to animal's class. Each object has state and behaviors. For example a dog has state:- color, name, height, age as well as behaviors:- barking, eating, and sleeping.
In How Many Ways To Create an Object ?
In 4 ways we can crete an Object:
1. Using new operator:
Ex: Employee obj=new Employee();
2.using factory method
Ex:NumberFormat obj=NumberFormat.getNumberInstance();
3.using newInstance() method:
Ex: Class c=Class.forName("Employee");
Employee obj=c.newInstance();
4 Using Cloning :Cloning means creating another copy sama as like original copy
Ex: Employee e2=e1.clone();
Using new operator:
classes are statically loaded with Java's "new " operator
Example program:
class Myclass
{
public static void main(String args[])
{
car c=new car();
}
}
Using Class.forName();
Dynamic loading is a techinique for programmatically invoking the functions of a class loader at run time.Let us look at how to load classes dynamically
.Class.forName(String className) //Static method which returns a Class
The forName(--) method in class-------Class
1.The find SystemClass(------) method in class-- ClassLoader.
2.The loadClass(--) method in class-----ClassLoader
3.When we call a newInstance() method,it always calls for the default constructor.So in Such a Case
bvwe must have a default construcor for a class.Otherwise we get an error
0 comments:
Post a Comment