OOPS in JAVA 2
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.
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. |
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )