Everything Classes
What is a class?
Think of a class as a blueprint for creating a object
Object is a instance of a class it is a real-world entity that contains attributes (data) and methods (functions)
When you call a class you are making a instance of it
Let’s use a example of a Car
The car is our class (blueprint)
It has attributes (data) of colour and model
It also has methods (functions) so a car can perform braking() , honking() etc
A attribute is a variable inside of a class that stores data about a object
A method is a function inside of a class that defines actions a object can perform
Encapsulation is the practice of hiding data inside of a class and allowing controlled access using methods
Encapsulation ensures that data remains secure and is not accidentally changed or modified
It also keeps the code maintainable by keeping related data and methods together
A private attribute is a attribute that cannot be accessed directly outside the class. COMMON MISTAKE is can be used inside a method inside of the class that can be called from outside. This is called a getter that retrieves a private attributes value.
A setter is a method that modifies a private attributes value ensuring its valid.
Inheritance helps use avoid repetition and makes our code more organised.
Example of inheritance imagine your building a system for different types of animals the parent class will be animals that will have name, age, eat(), sleep() but then you can have specific types of animals like dogs and cats with can inherit from the animal class so they automatically get the name, age attributes and the methods of eat and sleep but in their child classes dogs can have barking and cats meowing.
Polymorphism allows the same methods name in different classes but with different behaviour it helps to simplify code and makes it reusable. If you have a method makeSound() for both dog and cat but it behaves differently for both if you pass dog it barks and cat it meows