Home / Programming / Object Oriented Programing – What is a Class?
car class with function (or methods) and attributes

Object Oriented Programing – What is a Class?

A Class can be thought of as the blueprint of an Object.

Think about a house blueprint. You can’t live in it, but you can build a house from it, which means you build an instance of it. An Object is an instance of  a Class.

So what exactly is a class? it is a bunch of code that can contain methods, variables, loops, and all other types of code syntax.

As a convention Class names should be Proper cased and Camel cased

The following code defines a ‘Car’ class

// Java Code 
class Car{
 private String name; // member variable
 private String color;// member variable
 private int topSpeed;// member variable

//Constructor of a Class
 public Car(String name, String color, int topSpeed) { 
 this.name = name;
 this.color = color;
 this.topSpeed = topSpeed;
 }
/*
In object-oriented programming, a Constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
*/
}
// Kotlin Code. You Should learn Kotlin! it's concise
class Car(val name: String, val color: String, val topSpeed: Int)

class of a car - oop

from the above classes we can create many types of Car object from the ‘Car’ Class and define different characteristics for each.

we could do the following to Create an Object from our Defined Class.

//in java 
Car car1 = new Car ("polo", "Green",  200);
Car car2 = new Car ("mini", "Blue",  185);
Car car3 = new Car ("beetle", "Red",  165);
val car1 =  Car ("polo", "Green",  200)
val car2 =  Car ("mini", "Blue", 185)
val car3 =  Car ("beetle", "Red",  165)

car1, car2, car3 are our objects from’Car’ class.

Now we know that all Cars should at least posses the ability to be accelerate which leads us to Functions (or Methods)

car class with function (or methods) and attributes

 

 

About Edge Developer

Hey there! am Opeyemi Olorunleke (aka Edge Developer), an Android developer. I Love Sharing Android Tutorials and code snippets.

Check Also

recycler view multi select tutorial

RecyclerView Multi-Select using KOTLIN

Implementing multi-select on a Recycler View can be tricky and complicated. However, by the end …

Leave a Reply

Your email address will not be published. Required fields are marked *