Classes, Objects & Methods
Class in JAVA
A class in programming is like a blueprint for creating objects.
Imagine you're designing a new type of car. Before you can actually
build any cars, you need to create a detailed plan that outlines what
the car should look like, how it should function, and what features it
will have. This plan includes everything from the shape of the car to the
type of engine and the color of the seats.
Class is your detailed plan or blueprint.
Object is the actual car built from your plan.
Syntax of Class
class classname{
type instance-variable1;
type instance-variable2;
type methodname1(parameter-list){
//body of method
}
type methodname1(parameter-list);{
//body of method
}
}
Methods
• The data or variable defined within a class are called instance-variable.
• A method is a function that is defined inside a class. It describes what
an object can do or what actions it can perform.
• A class can have multiple methods.
A car can perform actions like starting, stopping, accelerating, and
honking. These actions can be represented as methods within the Car
class.
Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
type methodname1(parameters){
return value;
}
Here, value is the value returned.
Here type be any datatype like int, double float,boolean etc. And data
returned should be compatible with the method type.
Objects
• Objects are created from classes, which define their properties and
behaviors.
• To create a object of a class:
classname variable= new classname();
The class defines what attributes a car has (like color and speed) and
what actions it can perform (like start, stop, and honk).When you
create an object from this class, you are making a specific car with its
own set of characteristics.
Parameters and Arguments
• A parameter is a variable defined by the method that recieves a value
when a method is called.
int methodname(int variable1){
//body of method
return value;
}
Here variable1 is the parameter.
• An argument is a value that is passed to a method when it is invoked.
int variable2 = methodname(23);
Here 23 is the argument.
Constructors
• A constructor initializes an object immediately upon creation.
• The constructor is automatically called when the object is created.
• It has the same name as a class but syntax wise it is like a method.
class className{
className(){
//body of constructor
}
}
Parameterized Constructor
• In this type of constructor, value is passed during the initialization of
the object.
class className{
className(int para1,int para2,int para3){
//body of constructor
}
}
During initialization
className name= new className(arg1,arg2,arg3)
this Keyword
• the this keyword is a reference variable that refers to the current object—the object
whose method or constructor is being called.
• It is used to resolve naming conflicts, pass the current object as a parameter, and call
other constructors within the same class.
public class Car {
String color;
// Constructor with parameter
public Car(String color) {
[Link] = color; // '[Link]' refers to the instance variable, 'color' refers to the
parameter
}
}
Method Overloading
Method overloading is a feature in which multiple methods can have
the same name but differ in the type or number of their parameters.
int methodName(parameter1){
}
int methodName(parameter1,parameter2){
}
int methodName(parameter1,parameter2,parameter3){
}
JAVA MODIFIERS
Access Modifiers
MODIFIER DESCRIPTION
public This code is accessible to all classes.
private This code is only accessible within the declared class
default This code is only accessible in the same package.
protected The code is accessible in the same package and
subclasses.
Non-Access Modifiers
MODIFIER DESCRIPTION
final Attributes and methods cannot be
overridden/modified
abstract Can only be used in an abstract class, and can only be
used on methods.
static Attributes and methods belongs to the class, rather
than an object
Static
• When a member of a class is declared static, it can be accessed before any objets of its class are created.
• Both variables and methods can be declared static.
public class UseStatic {
static int a=3;
static int b;
static void meth(int x){
[Link]("x= "+x);
[Link]("a= "+a);
[Link]("b= "+b); }
static{
[Link]("Static block is initialized");
b=a*4; }
public static void main(String[] args) {
meth(4); }}
Final
• A field which is declared final cannot be modified.
• By coding convention the name of the identidiers should be in
UPPERCASE.
final double PI=3.14;
final int NUM=3;
final String NAME="JAVA";
Inheritence
• In Java, it is possible to inherit attributes and methods from one class
to another.
• Iheritence is divided into 2 categories:
• subclass (child) - the class that inherits from another class
• superclass (parent) - the class being inherited from
• To inherit from a class, use the extends keyword.
• It is useful for code reusability: reuse attributes and methods of an
existing class when you create a new class.