Classes-Objects in Java
Classes-Objects in Java
P.Prathibha
Topics for Today’s Session
About JAVA
Behaviours
Identity State / Attributes Bark
Breed Eat
Color
Age Sleep
Bite
An object is an instance of a class. A class is a template or blueprint from
which objects are created. So, an object is the instance(result) of a class.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
Class
A class is a group of objects which have common properties.
It is a template or blueprint from which objects are created.
It is a logical entity. It can't be physical.
Here
Everything inside the brackets is optional.
classname and superclassname are any valid java identifiers.
The keyword extends indicate that the properties of the superclass name are
extended to the classname class.
This concept is known as Inheritance.
Fields declaration:
Data is encapsulated in a class by placing data fields in the
class. These variables are called “instance variables”.
These are declared just as local variables;
Methods Declaration:
A class with only data fields has no life. Such classes cannot
respond to any messages.
We must add methods that are useful for manipulating the data
of the class.
Methods for a class are declared inside the body of the class
but immediately after the declaration of the variables.
Type methodname (parameter list) Method declaration having four parts:
{ 1. The name of the method ( method name) (It is any
method_body; valid identifier)
}
2. The type of the value of the method returns (type)
(it is any valid data type, it is void when it does not
return any value.)
Method in Java 3. A list of parameters (parameter list) (This list
In Java, a method is like a contains variable names and types of all the values
function which is used to we want to send as input, when no input data are
expose the behavior of an required, we must use empty parentheses)
object.
Advantage of Method 4. The body of the method. It describes the operations
Code Reusability to be performed on the data.
Code Optimization Example: : int sum (int a, int b)
{
c=a+b;
return c;
Local variables
Instance variable
Class variables
.
Creating Objects:
An object is a block of memory that contains space to store all the
instance variables.
An object is a software entity (unit) that combines a set of data with
set of operations to manipulate that data.
A class defines a type of object. i.e., each object belongs to some class
object is also called as an instance.
Java has an operator new to create objects. Creating an object is also
referred to as instantiating an object.
Objects in java can be created by using the syntax.
Syntax:
Classname objname; Example
Objname = new classname(); class Dog {
The first statement declares a variable to hold String breed;
the object reference and second one actually int age;
assigns the object reference to the variable. String color;
Ex void barking() {
triangle tri1; // declaring the object) }
Tri1 = new triangle //instantiating the object)
void hungry() {
The above two statements are combined into a }
single statement as void sleeping() {
}
Triangle tri1 = new triangle(); }
Accessing class Members: every object contains its own set of variables.
We should assign values to these variables in order to use them in our
program.
When we are outside the class we cannot access the instance variables and
methods directly. For this we use dot operator.
objectname.varaiblename = value;
objectname.methodname (parameter list);