0% found this document useful (0 votes)
20 views21 pages

Objects and Classes

A class describes an object and contains variables and methods. An object is an instance of a class that contains instance variables to represent its state and instance methods to define its behavior. Classes support encapsulation, inheritance, and polymorphism. Objects are created using the new keyword and initialized via constructors. Methods can be overloaded and classes provide getters and setters to access private variables.

Uploaded by

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

Objects and Classes

A class describes an object and contains variables and methods. An object is an instance of a class that contains instance variables to represent its state and instance methods to define its behavior. Classes support encapsulation, inheritance, and polymorphism. Objects are created using the new keyword and initialized via constructors. Methods can be overloaded and classes provide getters and setters to access private variables.

Uploaded by

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

OBJECTS AND CLASSES

The heart of object-oriented programming is to find


objects -- entities that have behaviors, that hold
information, and that can interact with one another.
Objects, Instance Methods, and Instance
Variables
Class
 A class describes an object.
 A class is used to create an object.
 A class is a kind of a blueprint.
 A class contains variables (attributes) and
subroutines (behavior).
Class
 Supports three key features of OOP:
 Encapsulation
 Inheritance
 Polymorphism
Instance Variables and Methods
 Instance variables or attributes
• define the state of an object in a class.
• can also be objects.
 Instance Methods
• functions that provide processing to the object’s data.
• determine the behavior of an object.
• The collection of all publicly available methods is
called the class interface.
Encapsulation
 Hides the implementation details of a class
 Forces the user to use an interface to access data
 Makes the code more maintainable
How to refer to instance variables and methods?

 The dot notation: <object>.<member>


 Syntax:
objectvariable.instancevariable
objectvariable.instancemethod
Object
 An object that belongs to a class is said to be an
instance of that class.
 The variables that the object contains are called
instance variables.
 The subroutines that the object contains are called
instance methods.
How Are Objects Created?
 An object is created when its is instantiated using
the new keyword.
 Calling new ...() causes the following events to
occur:
• memory is allocated for the new object and instance
variables are initialized to their default values
• explicit attribute initialization is performed
• the appropriate constructor is executed
A Class as a Type
 A class name can be used to specify the type of a
variable in a declaration statement, the type of a
formal parameter, or the return type of a function.

 Note: Declaring a variable does not create an


object!
Constructors and Object Initialization
Initializing Instance Variables
 An instance variable can be assigned an initial
value in its declaration, just like any other variable.
 An instance variable can be initialized using a
constructor.
Constructors
 A special type of subroutine.
 The definition of a constructor looks much like the
definition of any other subroutine, with three exceptions.
 A constructor does not have any return type (not even void).
 The name of the constructor must be the same as the name of
the class in which it is defined.
 And the only modifiers that can be used on a constructor
definition are the access modifiers public, private, and
protected. (In particular, a constructor can't be declared
static.)
The this reference
 When a method is invoked by an object, the object
reference is implicitly transmitted as a hidden first
argument to the method. Inside the method, the
reference has a name and it is called this.
Method Overloading
 Java allows the definition of two or more methods with the same
name within the same class provided that the order and type of
parameters are different.
 Example:
class Dummy {
public void display() {
}
public void display(int x) {
}
public void display(int x,char ch) {
}
}
Setters and Getters
Setter Method
 Allows “write access” to private variables.
 Also called the mutator method.
 The name of a setter method should consist of "set"
followed by a capitalized copy of the variable's
name, and it should have a parameter with the same
type as the variable.
Getter Method
 Also called the accessor method.
 It allows other classes to find out what are the
values of the variables.
 By convention, the name of an accessor method for
a variable is obtained by capitalizing the name of
variable and adding "get" in front of the name.
The Class “Object”
Class “Object”
 The root of all classes.
 It defines several instance methods that are
inherited by every other class.
 Two common methods:
1. public String toString()
2. public boolean equals(Object obj)

You might also like