Classes, objects and methods

Classes, objects and methods

 

Class is a user defined data type with template that serves to define its properties. Class is a collection of methods and data types of similar types encapsulated together for a specific functionality. Class is description of how to make an object that contains fields and methods . Java incorporates the basic OOPs concepts.

 

Java is a true object oriented language and therefore the underlying structure of all java programs is classes. Anything we wish to represent in a java program must be encapsulated in a class that defines the state and behaviour of the basic program components known as objects. Classes create objects , and objects use methods to communicate between them. This is all about object oriented programming structure.

Classes provide an efficient method for packing together a group of logically related data items and functions that work on them.

In java the data items are called fields and functions are called methods. Calling a specific method in an object is described as sending the object a message.

 

Creating an object

An Object in java is a block of memory that contains space to store all the instance variables. Creating an object is also known an instantiating an object.

For example

Rectangle rext1; // declare the object

Rect1 = new rectangle(); // instantiate the object

 

The first statement declares a variable to hold the object reference and the second one actually assigns the object reference to the variable. The variable rect1 is now an object of the rectangle class.

 

Note : Each object has its own copy of the instance variables of its class. Any changes to the variables of one object have no effect on the variables of the another . It is also possible to create two or more reference to the same object.

 

Example : Program illustrating the concept of classes nd objects.

 

Class rectangle

{

Int length, width; // declaration of method

Void getdata( int x , int y ) // definition of method 

{

Length = x .width = y;

}

Intrextarea() // another method

{

Int area = length * width ;

Return(area);

}

}

Class rectarea

{

 Public static void main( string args [])

{

Int area1, area2;

Rectangle rect1 = new rectangle(); // Creating objects

Rectangle rect 2 = new rectangle();     

Rect.length = 50; // accessing variables

Rect.width = 10 ;

Area1 = rect1.length x rect1.width;

Rect2.getdata(20,30);

Area2 = rect2.rectarea();

systesm.out.println(“Area : “ +area1);

System.out.println(“ area2 = “ + area2);

}

}

 

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control
Questions
Functional interfaces
Lambda functions
Method References in Java
Collections in Java
Java Spring
Java SpringBoot
Java SpringBoot
Spring Dependency Injection
Spring Inversion of Control
AOP
JavaBeans
Stream API