-
Notifications
You must be signed in to change notification settings - Fork 1
Classes and Objects
Up to this point, we've built programs with only 1 class. This class would contain some sort of methods that perform some tasks. Realistically, your program would be composed of many classes that specialize on specific things. Think of this as a car. For a car to work you need an engine, a transmission, some suspension, some wheels, etc, etc... However, all of these things (classes) perform different functions (methods). A transmission won't be able to perform the function of suspension and vice versa.
We'll get more into this through the section.
Simply put, a class is a template for objects. A class defines object properties including a valid range of values, and a default value.
An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings. In this case, think of the class as a blueprint of what any car is at a basic level - they all have some basic properties(that can vary a little) and basic functions. Objects, in this case, is when we use that blueprint to make an single car, and define the properties/functions of the object.
class Car{
...
}I.E. - we could have a car class that defines properties, like the color, the number of wheels or the number of doors, and also describes the object's behavior(functions), such as drive, park, and reverse. An object is a member or an "instance" of a class.
class Car{
int number_doors;
String color;
public void drive(){
...
}
public void park(){
...
}
public void reverse(){
...
}
}We know that for any variable , we can define the type and name of the variable as such : type name;
We can do the same with classes and thus create objects.
class Box{
int height;
int width;
public void printArea(){
...
}
}
class Main{
Box box = new Box(); //Based on the class we created , we just instantiated a new 'Box' object
}HOWEVER, there are some MAJOR points we have to touch up on before continuing.
Encapsulation simply means binding object state(fields) and behavior(methods) together. When we create a class, we are indirectly encapsulating the the properties and methods together.
The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.
class Car{
int number_doors;
String color;
}What does this mean? When an object is instantiated, we only want the user to be able to access methods/properties we deem fit to use.
class Car{
private int number_doors; //these properties of the Car object cannot be accessed outside of the class
private String color;
}
class Main{
Car car = new Car();
car.number_doors = 2; //THIS IS INVALID
}So How do we Encapsulate?
- Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
- Have getter and setter methods in the class to set and get the values of the fields.
class Car{
private int num_doors; //these properties of the Car object cannot be accessed outside of the class
private String color;
public void setColor(String c){
this.color = c; // the class's 'color' property will now be equal to the value passed in
}
public void setNumDoors(int n){
this.num_doors = n;
}
public void printDetails(){
System.out.println(this.color + " , "+ this.num_doors)
}
}
class Main{
Car car1 = new Car();
car1.number_doors = 2; //THIS IS INVALID. INSTEAD,
car1.setNumDoors(2); // This works
car1.setColor('Green');
Car car2 = new Car();
car2.setNumDoors(4);
car2.setColor('Black');
car1.printDetails();
car2.printDetails();
}A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
Think of them as getters and setters for properties, but they occur right when an object is defined/instantiated.
class Car{
private int num_doors; //these properties of the Car object cannot be accessed outside of the class
private String color;
public Car(int n, String c){
num_doors = n;
color = c;
}
public void printDetails(){
System.out.println(this.color + " , "+ this.num_doors)
}
}
class Main{
Car car1 = new Car(2, 'Green');
Car car2 = new Car(4, 'Black');
car1.printDetails();
car2.printDetails();
}