Familiar with data types like int, long, float?
If Yes, Then what is the limitation of these data types?
You got it right, they can only hold one type of data, i.e. and int can store integer values, a float can only store floating point values.
Question comes to our mind, how do we represent user defined types e.g. Person which can have a String name, a long phone number and an int age.
Class in java is way to go concept as it is used to create user defined types.
Benefit of user defined type is that it can combine and store multiple type and gives you the power to create a user defined type for anything in the real world.
We can divide them into two categories:
- JDK or System Class – They are those which comes along with JDK e.g. java.lang.String , java.lang.Thread
- Application Class-They are those which you create in your Java Program or application eg. Person, Student etc
Class in java is created using the keyword “class”
Except holding code for variables e.g. age, name and phone and methods e.g. getName(), class doesn’t do anything.
Real magic starts when you create object of class. As to represent every single person e.g. You,me and your friend are object of Person, they have different name, age and phone number but they are Person.
Object are actual thing which is created from blueprint called class. Just like the cars including BMW, Mercedes and Audi has blueprint and follow specification, every object follows structure defined in class.
In object-oriented programming, a class is a program code for creating objects, providing initial values for state and implementations of behaviour. In many languages, the class name is used as the name for the class, the name for the default constructor of the, and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.
An object when created by a constructor of the class, the resulting object is called an instance of the class, and the member variables specific to the object are called instance variables, to contrast with the class variables shared across the class.
Thus, an object is a specific instance of a class; it contains real values instead of variables.
The class is one of the defining ideas of object-oriented programming. Among the important ideas about classes are:
- A class can have subclasses which inherit all or some of the characteristics of the class. In relation to each subclass, the class becomes the superclass.
- Subclasses can also define their own methods and variables that may or may not be a part of their superclass.
- The structure of a class and its subclasses is called its class hierarchy.