0% found this document useful (0 votes)
24 views16 pages

5 - Class & Objects

Uploaded by

ankurbiswas.uem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views16 pages

5 - Class & Objects

Uploaded by

ankurbiswas.uem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CLASS & OBJECTS

CHAPTER - V
CLASS
• Class is the collection of objects.
• Class is not a real-world entity. It is just a template or blueprint or prototype.
• Class cannot occupy memory.
Syntax: eat() sleep()
access-modifier class ClassName
{
Animal
o Methods
o Constructors
o Fields
o Blocks dog cat
o Nested class…
}

05-02-2023 Ankur Biswas (ANB) 2


METHOD
• A set of codes which perform a particular task.
Syntax:
access-modifier return-type methodName(list of parameters)
{

05-02-2023 Ankur Biswas (ANB) 3


OBJECT
• Object is an instance of the class Create an object:
• Object is real-world entity i. new keyword
• Object occupies memory ii. newInstance() method
Object Consist of: iii. clone() method
1. Identity → Name of dog iv. deserialization
2. State/Attribute → Breed, age, colour v. factory methods
3. Behaviour → eat(), sleep(), [methods]

05-02-2023 Ankur Biswas (ANB) 4


OBJECT
Creating object using new keyword: Usage:
i. Declaration → Animal buzo 1. Call instance variable & non-static
ii. Instantiation → buzo = new Animal(); methods
iii. Initialization → constructor 2. Call constructors
Syntax: 3. Arrays
classname name_of_object=new constructor(); Initializing Objects:
object.method(); → buzo.eat(); 1. By reference variable
object.behaviour; → buzo.color=black 2. By method
3. By Constructors

05-02-2023 Ankur Biswas (ANB) 5


CLASSES & OBJECTS
• Object-oriented programming strictly separates the notion of what is to be done from how it
is done.
• “What” is described as a set of methods and their associated semantics.
• This combination – methods, data, and semantics – is often described as a contract between
the designer of the class and the programmer who use it.
• This contract defines a type such that all objects that are instances of the type are known to
honour that contract.
CLASSES & OBJECTS
• The “how” of an object defined by its class, which defines the implementation of the methods
the object supports.
• Each object is an instance of a class.
• When a method is invoked on an object, the class is examined to find the code to be run.
• An object can use other objects to do its job.
CLASSES & OBJECTS
• The actual memory allocation for the object of class is provided through instantiation using
new operator.
• The handle (of the object) to that memory location is called object reference.
• Object = data + {operations}
• In java these operations are defined in terms of methods.
• In object model, we access and update the data that resides in that class object only through
object reference.
• However, there are special cases that will be dealt separately in static fields and methods.
CLASSES & OBJECTS
• In object-oriented model, a program is not only a mere collection of isolated objects; the
important interactions and the relationships among these objects are accomplished by allowing
one object to hold the reference of another.
• The communication among objects is known as message passing.
CHARACTERISTICS OF AN OBJECT
• State: the primitive data within the class represents state of the object.
• A class designer provides two sections, namely attribute section and method section.
• The attribute section contains a set of type declarations. The types can either be primitive or
class/interface. Methods provide functionalities to these classes.
• The state of an object given is determined in terms of its actual value of individual type residing in it
after the creation of the object.
• The fact that every object has a state implies that every object consumes certain amount of memory
space.
CHARACTERISTICS OF AN OBJECT
• Behaviour: the functionality defined inside the class represents the class behaviour. The
behaviour of an object describes how an object responds to operations in terms of its state
updations and message passing.
• The signature of a method will be in the form of:
<return-type> <Method-Identifier-name>(<parameter-list>)
• If a method does not return any particular type, then we eclare the return type as void.
• When we design classes, we provide the methods that return the attribute values (also known as
accessors/get methods) and the methods that updates existing values (called as mutators/get methods).
CHARACTERISTICS OF AN OBJECT
• Identity: Objects may have same state and behaviour but they are separate individuals. Each
object has its own identity.

Teacher t1 = new Teacher(“Ankur”,”JAVA”);


Teacher t2 = new Teacher(“Ankur”,”JAVA”);
JAVA CLASSES
class A • Java can contain any number of classes
{ • At most one public class is allowed
} • If there is no public class, then the file may
class B have any name.
{ • If there is a public class, the file has to be
} saved as the class name.
class C
{
}
IMPORT
• Explicit Import • Implicit Import

java.util.ArrayList; java.util.*;

Explicit import is always recommended to In case we are importing implicitly, all the
improve the readability of the program. package and interfaces are available but not
the sub packages.
PACKAGES
• A group of related classes & interfaces are • Advantages:
grouped into a separate unit. • Naming conflicts may be resolved – e.g. Date
in java.sql & java.util
• Thus package is a encapsulation mechanism.
• Modularity of the application may be improved.
____________________________ • Maintainability of the application may be
Good programming practice is to create every improved.
class and interface as part of a package. • It provides security to all the components.
THANK YOU

05-02-2023 Ankur Biswas (ANB) 16

You might also like