What is Class ?

JAVA is object oriented technology because to represent total data in the form of object.
By using object reference variable we are calling all the methods, variables which is present in that class.
Class Main
{
......
   ......
}
Main m=new Main();

Main intention or you can say main purpose of classes in Java is to represent all real world entities in Java program in the form of classes like customer class, student class , employee class .
In simple word class is nothing but a container which contains other programing elements like variable , methods, constructor etc or you can say it is like a carry bag. So now you see how class is related with real world.
Many students find programing difficult ? Because they are missing that mapping , to map real world entities with Java programs. Every entity must have data and some behaviour or actions , to represent data of entity we have variables
Ex: int a =10;
"a" is variable of integer type holding 10 as a data.
Ex: For Student class we have data like student id , student name , student age , student class ,so for these number of data we will create variable .

Entity must have behaviour/actions like getStudentDetails , delete/update studentdetails , In Java these actions are represent in the form of methods.

Steps to create class in Java programs:
1.Declare a class by using "class" keyword.
2.Declare entity data in the form of variables.
3.Declare entity behaviours in the form of methods.
4.In main class,In main() method,create an object and reference variable for the class.
5.Access the members of the class by using reference variable.

Class Syntax:
[Access_Modifiers] class class Name[extends Super_Class][implements interface_List]
{
}

Note:-
1. Only public and <default> are allowed for the classes , private and protected are not allowed for the classes , because  access modifiers private and protected scopes are defined on the boundaries of classes so we can not apply them for the classes , rather they apply for the members of the classes
2. All the access modifiers like public,protected , default and private are allowed for the inner classes.
3. If we don’t mention the scope to the class , then  automatically the scope of the class is “default”
4. "extends" is a keyword , it can be used to specify super class in class syntax inorder to reuse the members of super class in the present subclass
5. "implements" is a java keyword , which can be used to specify one or more interface names in order to implement all the interface methods in the present class.

 
Class Elements:-
Class Test
{
1. variables ------- int a = 10;
2. methods ------- void add() {}
3. constructors------ Test() { }
4. instance blocks----- { }
5. static blocks------ static { }
}

Java codeing conventions for classes:
1. Class name start with upper case letter and every inner word starts with upper case letter.
2. This convention is also known as camel case convension.
Ex:- Student , StudentDetails etc

Now take one  example to understand clearly.

Example:
class Student
{
// Instance variables
int sid=101;
String sname=”Peter”;
String saddr=”london”;
int smarks=95;

//Instance methods
void get_Student_Details()
{
System.out.println(“****Student Details****”);
System.out.println(“Student ID:”+sid);
System.out.println(“Student name:”+sname);
System.out.println(“Student Marks :”+smarks);
System.out.println(“Student Address:”+saddr);
}
}
class Demo
{
public static void main(String[] args)
{
Student s=new Student();
s.get_Student_Details();
}
}

Output:
****Student Details****
Student ID:101
Student name:Peter
Student Marks:95
Student Address:london

Please run this example on your editor and do tell me your outputs.

On next post I will discuss on classes and objects. Thanku guys. Keep learning.

No comments:

Post a Comment

Principles of Object Oriented Programming (OOP)

This article aims to explain the principles of Object-Oriented Programming in Java, using simple way to make it easier  to understand the fu...