- What is a Class
- What happens when you create an object of a Class
- What are different types of classes in java
- Generics in Classes
What is a Class :
“A class is the blueprint from which individual objects are created.” – as per the java tutorials in Sun oracle documentation.
Simply put, a class is a digital skeleton of an external world object, which can store the state, and has a definite behavior. Using this skeleton we can create many objects in the Virtual environment (JVM), all of these objects will have the same behavior, with a state of their own.
Difference between, class and Class : In java, “class” with a small “c” letter is a keyword that is used to define a skeleton/Blueprint. A “Class” with a capital “C” letter is a generic type, that is the name of the class, which is used to create objects.
In the above Car Class, we have defined a couple of variables called “Brand”, “Color”. They store the state of the Car Objects. All the Car objects have a definite behavior that is defined as the methods “drive()”, “applyBreaks()”. The Car class is a logical structure of the real car entity.
The Objects created from the Car class (Skeleton) will have a state initialized at the time of creation of an Object for this class, using the constructor of the class (as shown in the code snippet below). Every Class defined with the “class” keyword, will have a Has-A relationship with the “Class” object in “java.lang” package.
Note : Class objects will created by the Classloader of the JVM, when an object of that type is instantiated.
What happens when you create an object of a Class
When an Object is created for any class, the JVM will create an Object for the Class(Car) type using the Classloader and then it will call the constructor to create a state for the object, and returns the reference of the object created for the Car type.
A Constructor is a way to create a new Object for any type. If there is no Constructor defined for the class, then the Compiler will add a default no-argument constructor into it. The first line of the Constructor has to be a “super()” or “this()” constructor. If there is no line like this, then the compiler will add the “super()” constructor by default. If a Constructor is defined, then the compiler will not add a no-arg constructor.
What are different types of classes in java
- Abstract Class
- Inner Class
- Local Class and Anonymous Class
- Static Class
This section is to give an introduction to various types of classes and not a comprehensive set of the features, of these classes.
Abstract Class : An Abstract Class is a class, where they will have at least one abstract methods. (Abstract methods are the that has no definition / body). These classes cannot be instantiated (Objects for these classes cannot be created).
Inner Class : An Inner Class is a class defined with in a class.
Local Class and Anonymous Class:
Local classes are a special type of Inner classes, that are defined in the body of a method in a class. Similarly, Anonymous classes are a a type of Local Classes defined inside a method without a class name.
Generics in Classes
Generics in Classes, helps to identify the Cast errors at the compile time, instead of capturing it late at runtime. Generics again is a vast concept, which I feel might need its own blog, hence limiting the discussion about it to the basics.
Generics allows to pass Type parameters to the classes, and are defined to restrict the attempt of using it to ineligible objects.
As shown in the above example, Box class ensures strict type checking with the objects created and hence will not result in any runtime issues, unless it is used in Raw type.
Hope, this is useful and you have got some information about the Java Classes. Please share your thoughts and suggestions in the comments section.