Object and Classes in Java

Nov 25 • Notes • 1940 Views • No Comments on Object and Classes in Java

We all know that Java is based on Object Oriented Programming (OOP) concept. The full form of OOP itself defines that the language Java is fully based on objects. These objects link Java to the real world models as one object represents one whole entity. Let us see it in details how it does. But before that we should know that object is also a class which is the largest parent class in Java. All the other classes inherit this object class by default.

OOPs in Java

Now let us discuss about the objects which represent a class and itself is not a class. They are actually the identifiable entities which represents a single unit with some characteristics and behavior. We can also say it as Class is a blue print of a particular type. For example: A dog is a class which has some states like – color, name, breed as well as behaviors -wagging, barking, eating, etc. But as seen earlier, an object is an instance of a class which has a close existence to real world model, so a Labrador, a Pekinese, etc. are the objects of the Class Dog because they have their particular real world existence.

Classes in Java:

Class is a kind of bag or box which contains things like data members and member functions. A class is defined by the keyword “class”. Variables that are defined inside methods, constructors or blocks are generally called local variables or properties. The variables which lies within a class but outside any method is known as Instance Variables. These variables can only be accessed from inside any method of that class. The variables which are declared within a class but outside any method with the static keyword is known as  Class variables. Every class has a constructor and it is necessary as it initializes the variables. It is actually a method of class with no return type. We need to define it. But if we do not, the compiler automatically builds a default constructor for that class and calls it. Every time when a new object is created, the constructor will be invoked separately for the new one. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.There are three steps when creating an object from a class that is declaration, instantiate and initialization.

Declaration: A variable declaration with a variable name with an object type.

Example:  Sample s ;

Instantiation: The “new” key word is used to create the object.

Example: Sample s = new ;

Initialization: The ‘new’ keyword is followed by a call to a constructor. This call initializes the new object.

Example: Sample s = new Sample ( ) ;

Example:

class Myclass

{

int sum = 0 ;

public void Sum ( int a , int b )

{

sum = a + b;

}

}

class MainClass

{

public static void main ( String ar[ ] ) throws IOExceptions

{

MyClass m = new MyClass ( )  ;

m . Sum (2,3) ;

}

}

I think I was able to clear out this topic. If you have any doubt or want to share your views, comments and suggestions regarding this topic, please do it in the comment box below.

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

« »