Class
Class is a template or blue print which is used to describe an object.
Class has got various definitions,
- Class is user defined data type.
- Class is an Abstract Data Type(ADT).
- Class is collection of variables and methods.
All the given definitions are really true but for a person who have just started to understand Java it becomes a bit tedious to understand.
Class is just a planning or a design on which object referring to it will be built upon.
Have you ever imagined why Fish cannot fly or Eagle cannot swim?
Answer is very simple because flying is not a characteristics of Fish and swimming is characteristics of Eagle.
If you understand this understanding concept of class will become more easier.
Remember a class is made up of following content and all of the are optional,because if you try to do something like this..
class Demo
{
}
it will compile fine.
So a class can be made up of,
1) Variables
2)Methods
3)Constructors
4)Inner Classes
so assume for example if a class A contains certain methods m1(),m2(),m3().
It will be only able to access those methods and not any other methods (Assuming class has not inherited any other class other that java.lang.Object)
So in simple words "AN OBJECT OF A CLASS CAN ACCESS ONLY THOSE METHODS WHICH ARE DEFINED INSIDE IT'S CLASS, NOTHING MORE OR NOTHING LESS".
So as discussed, class Eagle does not have a variable fin and method swim() hence it cannot swim but same are the variables and methods of Fish, hence it can swim().
Same way Fish does not have variable wing and method fly() hence it cannot fly, but same are the variables and methods of Eagle,so it can fly.
Now let us have a look at technical aspect of Class.
class is defined by a keyword class
class Fish
{
int fin;
Fish()
{
System.out.println("Inside constructor of Fish");
}
public void swim()
{
System.out.println("Swim of Fish");
}
}
0 comments:
Post a Comment