Introduction to Class in Java

Post navigation

Class is a template or blue print which is used to describe an object.

Class is one of the most important and misunderstood concept.

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 easier.

Remember a class is made up of following content and all of this 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”);

}

}­

Get Best Training from best Java Classes in Thane. i.e. Asterix Solution. Asterix Solution is best Java Classes in Thane providing industrial based quality training with live project working experience and personality development sessions and Interview Preparation Sessions to easily crack interviews. You can get more details on Java Training here : www.asterixsolution.com/java-training.html or

Search

Search for:

Text Widget

This is a text widget, which allows you to add text or HTML to your sidebar. You can use them to display text, links, images, HTML, or a combination of these. Edit them in the Widget section of the Customizer.