CLASSES, OBJECTS
AND METHODS
UNIT 1 – Chapter 5
1.1 INTRODUCTION
• Classes and objects are core part of java programming
language.
• The class is the essence of java.
• It is the foundation upon which the entire java
language is built.
• Class forms the basis for object Oriented
Programming, the data and methods are defined
inside a class.
• Java code is contained in either a class or an interface.
1.2 CLASS
01 02 03 04 05
All java programs A class is a It specifies both Java uses a class Thus, a class is
activity occurs template that the data and the to construct essentially a set
within a class. defines the form code that will objects and of plans and rules
of an object. operate on that objects are that specify how
data. instances of a to build an object.
class.
•There are two kinds of things we can include in a class definition,
• Data Members
• Methods
• Data Members: These are variables that store data items that typically differentiate one object of
the class from another.
• They are referred to as fields or member variables of a class.
• Methods: These define the operations you can perform for the class, they determine what we can
do with objects of the class.
• The fields in a class definition can be of any of the primitive data types, or they can be references
to objects of any class type.
Class = Data + Methods
5.3 DEFINING A CLASS
• When we define a class, we declare its exact form and nature.
• We specify the instance variables that it contains and the methods that operate on
them.
• A very simple class might contain only methods or only instance variables, but
most real-world classes contain both.
• A class is created by using keyword Class.
SYNTAX:
[accessModifier] class ClassName
// class body containing variables and methods
[ variable Declaration]
[ Method Declaration]
}
Public class Bicycle
{
/* Variable Declaration */
private int gear=5;
/* Method Declaration */
public void breaking()
{
System.out.println(“Working of Braking”);
}
}
Rules for defining a Class
By convention, the class
It must be a legal java There can be only one
names starts with a
identifier. public class.
capital letter.
If a class is to be defined
The name of the .java file A source file may have in a package, then the
must match with the any number of non-public package statement must
public class. classes. be the first statement in
the source file.
5.4 Adding
Variables to Class
Public class demo{
//declaring instance variables
Int age;
Float percentage;
//declaring class variables
Static int collegeCode;
CREATING
OBJECTS
• <classname> <reference_variable> = new <classname>
([arguments])
• Classname object = new classname();
• examples:
//Creating bicycyle objects
Bicycle sportsBicycle = new Bicycle();
Bicycle raceBicycle = new Bicycle();
Accessing Class Members
To access variables : To access Methods:
object.variables object.method()
Class A{
/* declaring instance variables */
Int I, j;
Public static void main (string[] args){
// Create object of class A
A a1 = new A();
a1.i = 10;
a1.j=20;
}
}
// Create another object of class A
A a2 = new A();
a2.i = 10;
a2.j=20;
CONSTRUCTORS
• A constructor is special method that is called when an
object is created.
• The purpose of a java constructor is to initialises newly
created objects before it is used.
• Unlike normal methods, a constructor has the same
name as that of class and it does not have any return
type.
Syntax: class ClassName{
ClassName() { //constructor
}
}
Types of constructors
• Default Constructor
• No Argument Constructor
• Parameterised Constructor
Default Constructor
• Default constructor is a constructor that is automatically created by java compiler
if it is not explicitly defined.
• If we do not create any constructor, the java compiler automatically create a no-
argument constructor during the execution of a program.
• We would not find default constructor in source code as it would be inserted into
the code during compilation and exists in .class file.
• Default constructor initializes any uninitialized instance variables with default
values.
class Bike1{
Bike1() // default constructor
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike1 b=new Bike1();
}
}
No Argument Constructor
• A constructor may or may not have any parameters.
• Constructors with no arguments is known as no-
arguments constructor.
• The signature is same as default constructor; however,
body can have any code unlike default constructor.
Parameterized
Constructor
• A java constructor can also accept one or more parameters.
• Constructors with arguments are known as parameterized
constructors.
• These parameters can then be used to dynamically initialize the
instance variables of the newly created object.
• Parameterized constructor is used to provide deferent values to
distinct objects.
package parameterConst;
public class Employee {
int empID;
String name;
// Parameterized Constructors
Employee(int empID, String name){
this.empID=empID;
this.name=name;
}
//Dispaly
void dispaly() {
System.out.println("Employee ID is "+empID + " Name " +name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1=new Employee(10,"Akash");
e1.dispaly();
}
• The keyword this has several uses in java, the
keyword this refers to the current object.
• Whenever it is required to point an object from a
functionality which is under execution then we use
Using this
the “this” keyword.
Keyword
• The job of this keyword is only to point, its always
point to an object that is executing the block in
which this keyword is present.
• The first use of “this” is to call constructor from another constructor, specifically
one in the current class.
Class A{
int i,j;
A(){
A(100); //error
}
A(int a){
}
}
Class A{
int i, j;
A(){
this(100); // calls constructor
which takes one argument
A(int a){
this(a,200); // calls constructor
which takes two argument
}
• The process of calling the constructor
from other constructors is called as
constrictor chaining
METHOD OVERLOADING
The “Static” Keyword
• The static keyword in java is used to share the
same variable or method of a given class.
• The users can apply static keyword with variables,
methods, blocks and nested classes.
• The static keyword belongs to the class than an
instance of a class.
• The static keyword is used for a constant variable
or a method that is same for every instance of a
class.
Static keyword can be
used with,
1. Variables
2. Methods
3. Blocks
4. Classes
a. Static Variables
• The instance variables are non-static and it is part of an object.
• But static variables are special type of variables that are not
associated with an object.
• Static variables are also called as class variables.
• The static variables can be accessed without an object.
• Class demo{
int x, y;
static int z; }
• Static variables are initialized only once, at the start of the execution.
• These variables will be initialized first, before the execution of main()
method.
• A static variable can be accessed directly by the class name and doesn’t need
any object.
<class_name>.<variable_name>
demo.z;
b. Static Methods
• A static method is associated with a class rather than the instances.
• The static methods are also called as class members.
• Static methods are appropriate when it is associated with a class rather than objects of that class.
• The most common example of a static member is main()
Class staticDemo{
int x, y;
static int z;
void static method1(){
system.out.println(z); } }
• A static method can be accessed directly by the class
name and doesn’t need any object.
<class_name>.<method_name>(arguments);
staticDemo.method1();
• A static method can access only static data, it cannot
access non-static data.
• A static method can call only other static methods and
cannot call a non-static method.
c. Static Blocks
• Sometimes a class will require some type of initialization before
it is ready to create objects, It might need to establish a
connection to a remote site.
• It also might need to initialise certain static variables.
• To handle these type of situations java allows us to declare a
static block.
• Static blocks are used to initialize the static variables.
• Static block syntax, static {
// variable initialization }
Example:
class Test{
// static variable
static int age;
// static block
static {
age=19;
}
}
d. Nested Static Class
1. Static Nested Classes
2. Non-Static Nested Classes
• A class can be made static only if it is a nested class.
• Nested static class doesn’t need a reference of outer class.
THANK YOU