Pages

Friday, September 9, 2016

What is class (java)

Class 
A class is the collection of data and functions,T
The data items and functions are defined within the class.
(data item : variable , constant etc.)
The functions taking task from the data items of a class each function has a unique relation ship with the data item of the class..

Defining a class :
The syntax for defining a class is as follow

class class_name {
body
}
 Class = this is the keyword. By which java compiler know’s that this       is a class.
Class_name : here we write the name of the class, it could be anything you want. But for better organization you can give a name so you can identify the class puposes easily.. for example for the student registration .. the name of the class should be (student_registration )

Body of the class , here we defines  members/data items and functions.

How to access a class
Mean to call the members of a class from another class is called access a class.
To access a class you have  to create an object for that first.

Syntax for object

class_name temporary name = new class_name() ;
where
class_name : represent the class . It should be written same as class name.
temporary name : represent a temporary name so you can use this name in the current class for calling members and function of  the class , For which you have created this object.
new : is the keyword it must be used in creating objects for class.
class_name() = again name of the class with two parenthesis.

For example an object for class (student) would be like this.
student std = new student() ;

If right now you have a class (student) and you want to access it from main class . You will do something like this.
class student{
public int a = 20 ;

};
class main{
//main method
public static void main(String[]args){
// create object for student class
student std = new student() ;

//print a from class student.
System.out.println(std.a) ;
}

};

The result will be like this.
20



If you have  a constructor in the class .. constructor will be discussed later.
Then creating object for only calling constructor is very simple and short.

Syntax for calling constructor.
New class_name() ;

Practical use of calling constructor of a class : to call student class from main class would be like this.

new student() ;


Public members, private members and protected members.
A class actually includes three types of members , but we’ll discussed two of them and the third one would be discussed later down the road.


Public members .
These type of class members can be accessed in the class and outside the class.Note if you don’t put any specifier befor the class member it would be public by default .

Public int id;

So this variable can be access and used in side and out side the class. Ie

salman

Private members.
These type of variable can only be access within the class. You cannot access private variable from outside the class.
To access and use private members of the class you have to have get mehod and set method within that class. Get method and set method will be discussed later ..

class student {
private int id = 3;
}
 class admin{
 public  student name ;
name = new student() ;
System.out.println(name.id);

}

Static :
Static members of a class or static methods of a class can be accessed and used in out side classes freely . the difference between public and static member is for the public member you have to create an object first and then call your member , and for the static member put the name of the class and a dot and then your member.
For example
class student{
// declare and initialize static variable
public static int a = 3 ;
public static int b = 3 ;

//create static methods
Public static void name(){
System.out.println(“your name is khan”);
}

};
 class call{
public static void main(String[]args){
System.out.println(student.a) ;
System.out.println(student.b) ;
Student.name();

}
};
The result will look like this.

1
2
Your name is khan.


Inner classes ;

This is  a type of nested class.
The class which is defined with in the other class is called inner class.

The inner class can accessed freely all the members of the outer class , however the outer class can’t access inner class members without creating an object..



1 comment:

  1. If any issue or proble please leave us a comment ! we'll try hard help you out !

    ReplyDelete