à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

OOPS in JAVA 2



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.

 

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.

 

 

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website