Class:
A class is a blueprint / template it has state and behavior. A class is a logical entity. The collection of objects is also known as class.
State: states means properties of class.
Behavior: Behavior means functionalities of a class.
A java class contains:
· Methods
· Data member
· Methods
· Class and interface
· Objects
Syntax:
Class <classname> {
Data meber;
Methods;
}
Example: Dog, Bus, Employee
Sample Program:
class Employee{
int id=1; //data member
String empname="Smiley"; //data member
void display(){ //method
System.out.println(" Id is "+id);
System.out.println("emp name"+empname);
}
public static void main(String args[]){
Employee e = new Employee(); //object of employee class
e.display();
}
}
Types of classes in
java:
There are two types of
classes.
1.
Inner classes
2.
Outer classes
Inner classes are again 4 types,
1.
Method Local
Inner Classes
2.
Static
Nested Classes
3.
Member
Inner Class
4.
Anonymous
Inner Classes
Method Local Inner
Classes:
Local inner classes are also
known as Method local inner classes. Local
inner classes are accessed inside of the block in which they are defined.
Static Nested
Classes:
A
class which is declared with static
keyword is known as Static Nested Class. Static classes can
be accessed by outer class without an object. We can't create an object for
inner classes.
Member Inner Class:
The Method-Local
Inner Classes have access to the specific instance of the enclosing classes
.
Anonymous Inner
Classes:
A
class without name is known Anonymous
Inner Classes and it doesn’t have constructor.
More about Wrapper class
More about Wrapper class
EmoticonEmoticon