JAVA.
We all come across a word class when we start reading java. So do you wanna know what is class? read the following carefully and completely to understand.
CLASS.
What is class?
A class is a collection of member, where the member can be member variable or member function. The member can be public or private.
Member variable of the class are called data member or attributes. Member functions in a class are called methods or behavior.
If we write any variable or function , it must be a member of some or other classes, so java is known as pure object oriented programming language.
A class in a java can be written using keywords.
A class with relative properties and operation is also known as abstract data types.
A class contains
- Data member.
- Methods.
- Constructor.
- Blocks.
- Classes and interface.
General form to define a class is:
[access specifier] class classname [extend superclassname] [implement interfacename]
{
[Data member]
[ Member function]
}
Every thing in the square bracket are optional.
Data member is nothing but adding variable to the program.
eg: string name;
int roll_no;
float salary;
Member function is nothing but adding method.
There are 4 basic part to declare.
- Type of the value the method returns.
- Name of the method
- List of parameter.
- Body of the method.
eg:
void getdata()
void display()
Object:
A class is nothing without Objects. because object are instances of classes.
An object unique entity, which contain data and function together in an object oriented programming language.
To create an object we use new operator.
The general form create a object is:
classname objectname = new classname();
example:
company office1 = new company();
company office2 = new company();
We can create any number of object. And also we can create two or more reference to the same object variable of another.
that is
company office1 = new company();
company office2 = office1;
Accessing class members.
Member of the class are accessible depending upon their accessibility condition.
If the member are private they cannot be accessed outside the class.
To access class member we must use the concerned object and the dot operator.
General form:
objectname.variablename = value;
objectname.methodname(parameter-list);
Example:
company office1 = new company();
company office2 = office1;
Comments
Post a Comment