The main component in the world of Object Oriented Programming (OOP) is a class. What is a class? A class is a blueprint for an object. Think of it like this. If a chocolate chip cookie is an object, the recipe for a cookie is the class. Classes create objects. All the data types that you have learned so far are objects and they have classes that give them properties. For example, when you say x = 5, you are making an object of the integer class with a value of 5.
Instances: Instances are basically the same thing as objects. When we write x = 5, we are really creating a new instance of the class integer with the value of 5 and the name of x.
Methods: Methods are functions that are specific to a class. Methods are written inside a class and can be used only for objects of that class. For example, when we say list.append(5), we are calling a method from the list class. We cannot simply say append(5) because only an object of list can use the append method.
Attributes: Anything specific to an object is called an attribute. For example, if I were to create a new class called Dog, then its attributes would be breed, size, color, weight, name, age, etc. We can also change the attributes of that Dog class. Each Dog object has the same attributes but different values associated with them. So if you wanted to make a new Dog object, you could have a different breed.
Copyright © 2024 PieceOfAPie - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.