Understanding the Concept of OOP’s: The Absolute Basic

Reading Time: 3 minutes

Object-Oriented Programming in short OOP every programmer in their programming career always faces this concept. We are going to cover the concept of OOPs in two parts. This is the first part where we are gonna talk about basic understanding of OOP and the concept of classes & objects.

What is OOP?

According to the wiki, OOP is a programming paradigm based on the concept of “objects”, let’s understand what does this means?

In Object-Oriented Programming we combine a group of related variables and functions into a unit, we call this unit an object. We refer to this variable as property and the function as a method. Let’s take an example in order to get a clear picture.

A car is an object with properties such as make, model, and color and methods like start(), stop(), and move().

Before going into the core concepts of OOPs let us first understand the concept of Classes and Objects

So Class is basically the blueprint of all the objects and Objects are basically the specific representation of Class. Let’s take an example in order to under this statement, consider the class as “Mobiles” and Samsung, Apple mobiles as objects of class Mobile. Now as we have understood the concept of class and objects let’s hop onto our favorites IDE and create Scala class and objects

Creating Scala Class

In Scala, a class contains the class keyword which is followed by the name of the class also there are some extra attributes that can be used with class declaration according to the requirement.

Object-Oriented Programming in short OOP every programmer in their programming career always faces this concept. We are going to cover the concept of OOPs in two parts. This is the first part where we are gonna talk about basic understanding of OOP and the concept of classes & objects.

What is OOP?

According to the wiki, OOP is a programming paradigm based on the concept of “objects”, let’s understand what does this means?

In Object-Oriented Programming we combine a group of related variables and functions into a unit, we call this unit an object. We refer to this variable as property and the function as a method. Let’s take an example in order to get a clear picture.

A car is an object with properties such as make, model, and color and methods like start(), stop(), and move().

Before going into the core concepts of OOPs let us first understand the concept of Classes and Objects

So Class is basically the blueprint of all the objects and Objects are basically the specific representation of Class. Let’s take an example in order to under this statement, consider the class as “Mobiles” and Samsung, Apple mobiles as objects of class Mobile. Now as we have understood the concept of class and objects let’s hop onto our favorites IDE and create Scala class and objects

Creating Scala Class

In Scala, a class contains the class keyword which is followed by the name of the class also there are some extra attributes that can be used with class declaration according to the requirement.

Syntax:

class Class_name{
// methods and fields
}

Let’s create a Scala program to illustrate how to create a class

// Name of the class
class smartphones{
  // Class variables
  val smartphonesBrand: List[String] = List("Apple", "Samsung", "OnePlus")
  // Class method
  def Display(): Unit =
  {
    println(smartphonesBrand)
  }
}
object Main
{
  // Main method
  def main(args: Array[String]): Unit = {
    // Class Object
    val obj = new smartphones()
    obj.Display()
  }
}

The output of this program will be:

List(Apple, Samsung, OnePlus)

Creating Objects in Scala

The objects in Scala can be accessed by invoking the method. An object consists of :

  • Identity: It’s a unique name that an object has and helps to interact with another object
  • Attributes: It reflects the properties of an object
  • Methods: It basically defines the behavior of an object

A new keyword is used in order to create an object. Syntax of creating an object in Scala is :

var obj = new smartphones() //from above example

Scala program for creating objects:

//Class with primary constructor
class iphone(name: Int, Storage: Int, iosVersion: String, color: String){

  println("iphone model name: " + name + " color: " + color)
  println("Storage in GB: " + Storage + " current ios version: " + iosVersion)
}
object Model{
  // Main method
  def main(args: Array[String]): Unit = {
    //Class Object
    var obj = new iphone(12, 128, "ios_version 15", "Blue")
  }
}

Output

iphone model name: 12 color: Blue
Storage in GB: 128 current ios version: ios_version 15

Explanation: In the above program, the class contains a single constructor (In Scala the body of the class is the body of the constructor and the class name is followed by the argument list).

The constructor in iPhone takes four arguments and the class object var obj = new iPhone(12, 128, “ios_version 15”, “Blue”) provides values for those arguments.

This was all about Classes and Objects in the second part we are going to cover the four pillars of OOPS.

If you enjoyed reading this blog and want to get more knowledge in Scala…Click Here

Refrences/Links

https://docs.scalalang.org/tour/classes.html#:~:text=Classes%20in%20Scala%20are%20blueprints,covered%20later%20in%20the%20tour.

Written by 

Hi community, I am Raviyanshu from Dehradun a tech enthusiastic trying to make something useful with the help of 0 & 1.

Leave a Reply