xxxxxxxxxx
The Object is the real-time entity having some state and behavior.
In Java, Object is an instance of the class having the instance variables
as the state of the object and the methods as the behavior of the object.
The object of a class can be created by using thenewkeyword
xxxxxxxxxx
Objects are the collection of properties which contain values as key:value pairs.
A property is also like a variable which stores data, but in property we stores
data in key:value pair i.e, age:21
We access property of an objects by object_name.property_name
xxxxxxxxxx
# Define a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I'm {self.age} years old.")
# Create an object (instance) of the Person class
person = Person("John", 25)
# Access object properties
print(person.name) # Output: John
print(person.age) # Output: 25
# Call object methods
person.greet() # Output: Hello, my name is John and I'm 25 years old.
xxxxxxxxxx
// JavaScript objects are a fundamental data structure used to represent various entities.
// They have properties that can be used to store key-value pairs of data.
// Create an object using object literal notation
const person = {
name: "John",
age: 25,
city: "New York"
};
// Accessing object properties
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 25
// Adding a new property to the object
person.gender = "Male";
// Updating an existing property
person.age = 26;
// Deleting a property
delete person.city;
// Looping through object properties
for (let key in person) {
console.log(key + ": " + person[key]);
}
// Output:
// name: John
// age: 26
// gender: Male
xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
# Creating an object/instance of the Person class
person1 = Person("John", 25)
# Accessing object attributes and invoking methods
print(person1.name) # Output: John
print(person1.age) # Output: 25
person1.introduce() # Output: My name is John and I am 25 years old.