Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Object-Oriented Programming (OOP) – Building Real-World Applications

In the previous article, we learned about Error Handling and Debugging. As applications grow larger, managing code becomes more difficult. Imagine creating software for:

  • A school management system

  • A banking application

  • An online shopping website

  • A hospital management system

Writing everything using variables and functions alone becomes complicated.

To solve this problem, programmers use Object-Oriented Programming (OOP).

OOP helps organize code into objects that represent real-world entities.

In this article, we will learn:

  • What OOP is

  • Why OOP is important

  • Classes and Objects

  • Constructors

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

  • Real-world examples


What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects.

An object contains:

Properties (Data)

Methods (Functions)

Real-Life Example

Consider a Car.

A car has:

Properties

Brand

Color

Model

Speed

and

Behaviors

Start()

Stop()

Accelerate()

In OOP:

Car → Object

Why OOP is Needed

Without OOP:

let name = "Rahul";

let age = 20;

function study()
{
}

Managing thousands of students becomes difficult.


With OOP:

Student Object

contains everything related to a student.


Benefits of OOP

  • Better organization

  • Reusable code

  • Easier maintenance

  • Real-world modeling

  • Reduced duplication


What is a Class?

A class is a blueprint for creating objects.

Think of it as a design.

Example:

Blueprint of House
      ↓
Many Houses Created

Similarly:

Class
      ↓
Objects

Class Syntax

class Student
{

}

This creates a class named Student.


What is an Object?

An object is an instance of a class.

Example:

let s1 =
new Student();

Here:

Student → Class

s1 → Object

Class and Object Relationship

Class

↓

Object 1

Object 2

Object 3

One class can create many objects.


Creating a Simple Class

Example:

class Student
{
    name = "Rahul";

    age = 20;
}

Creating Object

let s1 =
new Student();

console.log(
s1.name
);

Output:

Rahul

Complete Example

class Student
{
    name = "Rahul";

    age = 20;
}

let s1 =
new Student();

console.log(
s1.name
);

console.log(
s1.age
);

Output:

Rahul

20

Constructor Method

A constructor initializes object data.

It runs automatically when an object is created.


Syntax

constructor()
{

}

Example

class Student
{
    constructor()
    {
        console.log(
        "Object Created"
        );
    }
}

let s1 =
new Student();

Output:

Object Created

Constructor with Parameters

class Student
{
    constructor(name,age)
    {
        this.name = name;

        this.age = age;
    }
}

Creating Objects

let s1 =
new Student(
"Rahul",
20
);

let s2 =
new Student(
"Amit",
22
);

Accessing Data

console.log(
s1.name
);

Output:

Rahul

Understanding this Keyword

this refers to the current object.

Example:

this.name

means:

Current Object's Name

Complete Constructor Example

class Student
{
    constructor(name,age)
    {
        this.name = name;

        this.age = age;
    }
}

let s1 =
new Student(
"Rahul",
20
);

console.log(
s1.name
);

console.log(
s1.age
);

Output:

Rahul

20

Methods in Classes

Methods define object behavior.

Example:

class Student
{
    greet()
    {
        console.log(
        "Hello"
        );
    }
}

Calling Method

let s1 =
new Student();

s1.greet();

Output:

Hello

Example with Data

class Student
{
    constructor(name)
    {
        this.name = name;
    }

    greet()
    {
        console.log(
        "Hello " +
        this.name
        );
    }
}

let s1 =
new Student(
"Rahul"
);

s1.greet();

Output:

Hello Rahul

Four Pillars of OOP

Object-Oriented Programming is based on:

1. Encapsulation

2. Inheritance

3. Polymorphism

4. Abstraction

1. Encapsulation

Encapsulation means:

Binding Data and Methods Together

inside a single unit (object).


Example

class BankAccount
{
    constructor(balance)
    {
        this.balance =
        balance;
    }

    deposit(amount)
    {
        this.balance +=
        amount;
    }
}

Data and methods are combined.


Benefits of Encapsulation

  • Data protection

  • Better security

  • Improved maintainability


2. Inheritance

Inheritance allows one class to acquire properties and methods of another class.


Real-Life Example

Animal

↓

Dog

Dog inherits characteristics of Animal.


Syntax

class Dog
extends Animal
{

}

Example

class Animal
{
    eat()
    {
        console.log(
        "Eating"
        );
    }
}

class Dog
extends Animal
{

}

Using Inherited Method

let d =
new Dog();

d.eat();

Output:

Eating

Why Inheritance?

Without inheritance:

Same Code Repeated

With inheritance:

Code Reused

Example

class Animal
{
    eat()
    {
        console.log(
        "Eating"
        );
    }
}

class Dog
extends Animal
{
    bark()
    {
        console.log(
        "Barking"
        );
    }
}

let d =
new Dog();

d.eat();

d.bark();

Output:

Eating

Barking

3. Polymorphism

Polymorphism means:

One Interface

Multiple Forms

Example

class Animal
{
    sound()
    {
        console.log(
        "Animal Sound"
        );
    }
}

Overriding Method

class Dog
extends Animal
{
    sound()
    {
        console.log(
        "Bark"
        );
    }
}

Example

let d =
new Dog();

d.sound();

Output:

Bark

Method behavior changed.


4. Abstraction

Abstraction means:

Showing Important Details

Hiding Internal Details

Real-Life Example

Car Steering

You use it without knowing engine mechanics.


Example

class Car
{
    start()
    {
        console.log(
        "Car Started"
        );
    }
}

User only uses:

car.start();

Internal details remain hidden.


Getter and Setter Methods

Used to control access to data.


Getter

get name()
{
    return this._name;
}

Setter

set name(value)
{
    this._name =
    value;
}

Example

class Student
{
    set name(value)
    {
        this._name =
        value;
    }

    get name()
    {
        return this._name;
    }
}

let s =
new Student();

s.name =
"Rahul";

console.log(
s.name
);

Output:

Rahul

Practical Program – Employee Class

class Employee
{
    constructor(
    name,
    salary
    )
    {
        this.name =
        name;

        this.salary =
        salary;
    }

    display()
    {
        console.log(
        this.name
        );

        console.log(
        this.salary
        );
    }
}

let emp =
new Employee(
"Amit",
50000
);

emp.display();

Output:

Amit

50000

Practical Program – Bank Account

class BankAccount
{
    constructor(balance)
    {
        this.balance =
        balance;
    }

    deposit(amount)
    {
        this.balance +=
        amount;
    }

    showBalance()
    {
        console.log(
        this.balance
        );
    }
}

let account =
new BankAccount(
1000
);

account.deposit(
500
);

account.showBalance();

Output:

1500

Complete OOP Program

<!DOCTYPE html>

<html>

<body>

<script>

class Student
{
    constructor(name,age)
    {
        this.name = name;

        this.age = age;
    }

    show()
    {
        document.write(
        this.name
        );

        document.write("<br>");

        document.write(
        this.age
        );
    }
}

let s1 =
new Student(
"Rahul",
20
);

s1.show();

</script>

</body>

</html>

Output:

Rahul

20

OOP vs Traditional Programming

TraditionalOOP
Function BasedObject Based
Less ReusableHighly Reusable
Hard to ManageEasy to Manage
Less SecureMore Secure

Real-Life Applications

OOP is used in:

  • Banking systems

  • School management systems

  • Hospital software

  • Games

  • E-commerce applications

  • Mobile applications

Frameworks like Angular and libraries like React heavily use OOP concepts.


Common Mistakes Beginners Make

Forgetting new Keyword

Wrong:

let s =
Student();

Correct:

let s =
new Student();

Misusing this

Wrong:

name = name;

Correct:

this.name = name;

Not Understanding Inheritance

Remember:

Child Class

inherits

Parent Class

Advantages of OOP

  • Reusable code

  • Better organization

  • Easy maintenance

  • Real-world representation

  • Improved scalability


Conclusion

Object-Oriented Programming is one of the most important concepts in modern JavaScript development. It helps developers create organized, reusable, and scalable applications.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...