Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

Classes and Objects (Java)

1. A class is a blueprint that defines common attributes and behaviors of objects. It contains data fields and methods. 2. An object is an instance of a class - a real world entity that has state, behavior, and identity. Objects are created from classes using the 'new' keyword. 3. Classes and objects have real world parallels - for example, a Dog class could have objects representing individual dogs with attributes like name, color, and behaviors like barking. Creating objects allows programs to model real world things.

Uploaded by

Abdulrehman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Classes and Objects (Java)

1. A class is a blueprint that defines common attributes and behaviors of objects. It contains data fields and methods. 2. An object is an instance of a class - a real world entity that has state, behavior, and identity. Objects are created from classes using the 'new' keyword. 3. Classes and objects have real world parallels - for example, a Dog class could have objects representing individual dogs with attributes like name, color, and behaviors like barking. Creating objects allows programs to model real world things.

Uploaded by

Abdulrehman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Classes and Objects (Java)

Walia Fatima
Contents
• What is a class, object, attribute and method?
• Creating instance of class
• Example
What is class?
• A class is a programmer-defined, self-contained, reusable software
entity that mimics a real world thing.
• A class is a 3-compartment box containing the name, state, and the
methods.
Class Name

Attributes

Methods

• An instance is an instantiation of a particular item of a class.


What is class?
• A class is a group of objects which have common properties.

• It is a user-defined template or blueprint from which objects are


created.

• A class in java is a logical entity only.


What is class?
• It represents the set of properties or methods that are common to all
objects of one type.

• For-example: car

• The car has an attribute such as, weight and color, and methods, such as
drive and brake.

• A class is like an object constructor, or a “blueprint” for creating objects.


Syntax and code example
• Syntax Code
access_modifier class <class_name> class Car
{ {
fields; int weight;
methods; String color;
}
void brake(){}
void drive(){}
}
What is an Object?
• Usually a person, place or thing (a noun). An object is an instance of
class.

• Object is considered to be partitioned area of computer memory that


stores data and set of operations that can access the data.

• An object is a real world entity.


What is an Object?
• The object is an entity which has state, behavior and identity.

• The object is an instance of a class.


Object has 3 characteristics
• State: represent the data (value) of an object.

• Behavior: represent the behavior (functionality) of an object such as


withdraw etc.

• Identity: An object identity is typically implemented via a unique id.


The value of the id is not visible to external user. However, it is used
internally by the JVM to identify each object uniquely.
What is an object?
• An object in Java is the physical as well as a logical entity, whereas a
class in Java is a logical entity only.

• An entity that has state and behavior is known as object e.g. chair,
bike, marker, pen, table etc.
Real world examples of Class and Object
• In real world many examples of object and class like dog, cat, and cow
are belong to animal's class.
• Each object has state and behaviors.
• For example a dog has state:- color, name, height, age as well as
behaviors:- barking, eating, and sleeping.
Real world examples of Class and Object
• Vehicle class
Car, bike, truck these all are belongs to vehicle class. These Objects have
also different states and behaviors. For Example car has state - color,
name, model, speed, Mileage. as well as behaviors - distance travel
Creating an Object
• In Java, the new key word is used to create new objects.

• There are three steps when creating an object from a class:

• Declaration: A variable declaration with a variable name with an object type.

• Instantiation: The 'new' key word is used to create the object.

• Initialization: The 'new' keyword is followed by a call to a constructor. This call


initializes the new object.
Methods
• A set of codes which perform a particular task.
• Syntax
access_modifier returntype methodName(list of parameters)

You might also like