Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Spotle.ai Study Material
Spotle.ai/Learn
Class And Object
in Java
Spotle.ai Study Material
Spotle.ai/Learn
A class is a data type in Java that
defines a template or blueprint for
an object. In other words a class
refers to the category or type of
objects. So your cat Kitty is of
type cat, your red Mercedes is of
type car and your bank account is
of type savings account. A class
has variables defining properties
and methods defining behaviour
of its objects.
Class
Image Source: Wikipedia
2
Spotle.ai Study Material
Spotle.ai/Learn
Class Syntax
3
Public Class Cat
{
colour;
breed;
getBreed()
meow()
eat()
}
Variables
Methods
Name of
the class
Spotle.ai Study Material
Spotle.ai/Learn
A variable is a named
field containing
information associated
with the class or object.
For example the colour
variable of Class Cat
indicates the colour of
the specific cat. A
variable has a data type
indicating what type of
variable (number, text,
array, custom, etc) it is.
What Are Variables?
4
Colour
= Black
Colour
= White
Spotle.ai Study Material
Spotle.ai/Learn
Types Of Variables
5
Local
Variables
• A variable declared inside
a class method/
constructor is called a
local variable.
• Other methods in the
class are not aware of
these variables.
• For example, consider the
drink method of class Cat:
drink()
{float milk_qty = 10}
milk_qty is a local
variable of method
drink().
Instance
Variables
• Are variables declared
inside a class but outside
of any method or
constructor.
• These are called instance
variables because their
values are instance
specific and are not
shared among instances.
Eg. the colour variable of
the Class Cat.
Class
Variables
• Are variables declared
inside a class with the
keyword static.
• A single copy of a static
variable is shared among
all instances of a class.
• Static variables are used
to declare common
properties – for example
the number of legs of a
cat or the mileage of a
brand of cars.
Spotle.ai Study Material
Spotle.ai/Learn
What Is A Class?
6
What is your
colour?
But you cannothide your
no_of_legs. Thatis a static variablein Class Cat!
Instance And Static Variables
My colour is my
colour. None of
your colour!
Spotle.ai Study Material
Spotle.ai/Learn
Methods are used to describe
behaviour of the objects of a class.
A method is a set of statements
which is referred to by name and
can be invoked at any point in a
program. It performs a logical unit
of work.
For example get_mileage() method
in class Car is used to return a
specific car’s mileage. In our cat
example, meow() and drink_milk
are methods of the cat objects.
What Are Methods?
7
Meow()
Meow()
Spotle.ai Study Material
Spotle.ai/Learn
Public int getMileage()
{
return mileage;
}
Public void refuel(float fuel)
{
total_fuel = total_fuel + fuel;
}
Syntax Of A Method
8
Return type indicates
the data type of the
variable returned by a
method.
This indicates the
parameter with which
the method is invoked
from within a program.
This is the method
name.
The body of the
method describes the
functionality.
Spotle.ai Study Material
Spotle.ai/Learn
What Are Constructors?
9
Constructor is a block of code defined
in a class that initializes the newly
created object.
A constructor is structurally very
similar to an instance method in Java
but it is not a method as it doesn’t
have a return type.
A constructor is automatically called
when the instance of a class is
created.
Public class Cat
{
Cat()
{
System.out.println(“Meow !I
am a new Cat.”)
}
}
Definition Example
Spotle.ai Study Material
Spotle.ai/Learn
Types Of Constructors
10
If a class does not have
any constructor, Java
compiler inserts a default
constructor into the code.
A default constructor, for
class Cat, looks like this.
Note the body is empty:
Cat()
{
}
The default constructor is
not inserted if you
implement a constructor
for your class.
A no-argument
constructor is one that
takes no arguments or
parameters.
For example:
Cat()
{
System.out.println(“Meow
!I am a new Cat.”)
}
}
Default
Constructors
Parameterized
Constructors
No Argument
Constructors
A constructor with
arguments or parameters
is known as a
Parameterized
constructor.
For example:
Cat( String msg)
{
System.out.println(“Meow
!I am a new Cat with a
special message” + msg)
}
}
Spotle.ai Study Material
Spotle.ai/Learn
Class Syntax - Revisited
11
public class Cat
{
string colour;
string breed;
static no_of_legs = 4
Cat()
{
System.out.println(“Meow !I
I am a new Cat.”)
}
public void meow()
{
System.out.println(“Meow on demand!”);
}
}
Variables
Methods
Name of
the class
Constructors
Spotle.ai Study Material
Spotle.ai/Learn
What Is An Object?
Think of the class like the Mercedes blue-print
and the object like your brand new Mercedes.
12
Object is an instance of a class.
An object is a self-contained
component with methods or
behaviour and properties or state.
Objects are the real world
manifestation of a class like your
house is the manifestation of the
architect’s blue-print. Or your car is a
real object of type car.
Objects occupy memory location at
run-time.
Spotle.ai Study Material
Spotle.ai/Learn
Creating An Object In Java
13
Cat myKitty = new Cat()
Cat myKitty is the declaration of the
object myKitty of class Cat.
Objects are created by using the
operator New which allocates memory
for the object.
The Cat() command invokes the no-arg
constructor we defined earlier (In Slide
11).
So when Cat myKitty = new Cat()
is run, the following gets printed:
Meow! I am a new Cat.
Spotle.ai Study Material
Spotle.ai/Learn
Class Object
A class is a data type.
It defines a template
or blueprint for an
object.
Object is an instance
of a class.
Does not occupy
memory location.
Occupies memory
location at run time.
Does not really exist
as it only provides a
template. You can
therefore not perform
operations on a class.
You perform
operations on objects.
Class Vs Object
14
Think of the class like the Mercedes blue-print
and the object like your brand new Mercedes.

More Related Content

Class and Objects in Java

  • 2. Spotle.ai Study Material Spotle.ai/Learn A class is a data type in Java that defines a template or blueprint for an object. In other words a class refers to the category or type of objects. So your cat Kitty is of type cat, your red Mercedes is of type car and your bank account is of type savings account. A class has variables defining properties and methods defining behaviour of its objects. Class Image Source: Wikipedia 2
  • 3. Spotle.ai Study Material Spotle.ai/Learn Class Syntax 3 Public Class Cat { colour; breed; getBreed() meow() eat() } Variables Methods Name of the class
  • 4. Spotle.ai Study Material Spotle.ai/Learn A variable is a named field containing information associated with the class or object. For example the colour variable of Class Cat indicates the colour of the specific cat. A variable has a data type indicating what type of variable (number, text, array, custom, etc) it is. What Are Variables? 4 Colour = Black Colour = White
  • 5. Spotle.ai Study Material Spotle.ai/Learn Types Of Variables 5 Local Variables • A variable declared inside a class method/ constructor is called a local variable. • Other methods in the class are not aware of these variables. • For example, consider the drink method of class Cat: drink() {float milk_qty = 10} milk_qty is a local variable of method drink(). Instance Variables • Are variables declared inside a class but outside of any method or constructor. • These are called instance variables because their values are instance specific and are not shared among instances. Eg. the colour variable of the Class Cat. Class Variables • Are variables declared inside a class with the keyword static. • A single copy of a static variable is shared among all instances of a class. • Static variables are used to declare common properties – for example the number of legs of a cat or the mileage of a brand of cars.
  • 6. Spotle.ai Study Material Spotle.ai/Learn What Is A Class? 6 What is your colour? But you cannothide your no_of_legs. Thatis a static variablein Class Cat! Instance And Static Variables My colour is my colour. None of your colour!
  • 7. Spotle.ai Study Material Spotle.ai/Learn Methods are used to describe behaviour of the objects of a class. A method is a set of statements which is referred to by name and can be invoked at any point in a program. It performs a logical unit of work. For example get_mileage() method in class Car is used to return a specific car’s mileage. In our cat example, meow() and drink_milk are methods of the cat objects. What Are Methods? 7 Meow() Meow()
  • 8. Spotle.ai Study Material Spotle.ai/Learn Public int getMileage() { return mileage; } Public void refuel(float fuel) { total_fuel = total_fuel + fuel; } Syntax Of A Method 8 Return type indicates the data type of the variable returned by a method. This indicates the parameter with which the method is invoked from within a program. This is the method name. The body of the method describes the functionality.
  • 9. Spotle.ai Study Material Spotle.ai/Learn What Are Constructors? 9 Constructor is a block of code defined in a class that initializes the newly created object. A constructor is structurally very similar to an instance method in Java but it is not a method as it doesn’t have a return type. A constructor is automatically called when the instance of a class is created. Public class Cat { Cat() { System.out.println(“Meow !I am a new Cat.”) } } Definition Example
  • 10. Spotle.ai Study Material Spotle.ai/Learn Types Of Constructors 10 If a class does not have any constructor, Java compiler inserts a default constructor into the code. A default constructor, for class Cat, looks like this. Note the body is empty: Cat() { } The default constructor is not inserted if you implement a constructor for your class. A no-argument constructor is one that takes no arguments or parameters. For example: Cat() { System.out.println(“Meow !I am a new Cat.”) } } Default Constructors Parameterized Constructors No Argument Constructors A constructor with arguments or parameters is known as a Parameterized constructor. For example: Cat( String msg) { System.out.println(“Meow !I am a new Cat with a special message” + msg) } }
  • 11. Spotle.ai Study Material Spotle.ai/Learn Class Syntax - Revisited 11 public class Cat { string colour; string breed; static no_of_legs = 4 Cat() { System.out.println(“Meow !I I am a new Cat.”) } public void meow() { System.out.println(“Meow on demand!”); } } Variables Methods Name of the class Constructors
  • 12. Spotle.ai Study Material Spotle.ai/Learn What Is An Object? Think of the class like the Mercedes blue-print and the object like your brand new Mercedes. 12 Object is an instance of a class. An object is a self-contained component with methods or behaviour and properties or state. Objects are the real world manifestation of a class like your house is the manifestation of the architect’s blue-print. Or your car is a real object of type car. Objects occupy memory location at run-time.
  • 13. Spotle.ai Study Material Spotle.ai/Learn Creating An Object In Java 13 Cat myKitty = new Cat() Cat myKitty is the declaration of the object myKitty of class Cat. Objects are created by using the operator New which allocates memory for the object. The Cat() command invokes the no-arg constructor we defined earlier (In Slide 11). So when Cat myKitty = new Cat() is run, the following gets printed: Meow! I am a new Cat.
  • 14. Spotle.ai Study Material Spotle.ai/Learn Class Object A class is a data type. It defines a template or blueprint for an object. Object is an instance of a class. Does not occupy memory location. Occupies memory location at run time. Does not really exist as it only provides a template. You can therefore not perform operations on a class. You perform operations on objects. Class Vs Object 14 Think of the class like the Mercedes blue-print and the object like your brand new Mercedes.