Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Object-Oriented Programming

Object-Oriented Programming (OOP) with Free code

Posted on August 28, 2024August 28, 2024 By Updategadh No Comments on Object-Oriented Programming (OOP) with Free code

Object-Oriented Programming (OOP) concepts like classes, inheritance, encapsulation, and object interaction. Below are the solutions for each of the given scenarios:

Table of Contents

  • Object-Oriented Programming (OOP) concepts like classes, inheritance, encapsulation, and object interaction. Below are the solutions for each of the given scenarios:
    • 1. Create a Class: Book
    • 2. Bank Account Simulation: BankAccount
    • 3. Basic Inheritance: Animal, Dog, Cat
    • 4. Encapsulation: Student
    • 5. Object Interaction: Library and Book

1. Create a Class: Book

Task: Define a class named Book that has attributes like title, author, and pages. Write a method display_info() that prints out the book’s details.

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def display_info(self):
        print(f"Title: {self.title}")
        print(f"Author: {self.author}")
        print(f"Pages: {self.pages}")

# Example usage:
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", 180)
book1.display_info()
# Output:
# Title: The Great Gatsby
# Author: F. Scott Fitzgerald
# Pages: 180
https://updategadh.com/python/object-oriented-programming/

2. Bank Account Simulation: BankAccount

Task: Extend the BankAccount class by adding a method transfer() that allows transferring money from one account to another.

class BankAccount:
    def __init__(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited: {amount}")

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
            print(f"Withdrawn: {amount}")
        else:
            print("Insufficient funds")

    def transfer(self, amount, other_account):
        if amount <= self.balance:
            self.withdraw(amount)
            other_account.deposit(amount)
            print(f"Transferred: {amount} to Account {other_account.account_number}")
        else:
            print("Insufficient funds for transfer")

# Example usage:
account1 = BankAccount("123456", 500)
account2 = BankAccount("789101", 300)

account1.transfer(200, account2)
# Output:
# Withdrawn: 200
# Deposited: 200
# Transferred: 200 to Account 789101

3. Basic Inheritance: Animal, Dog, Cat

Task: Create a base class Animal with attributes name and species and a method make_sound(). Create two derived classes Dog and Cat that override the make_sound() method with their respective sounds.

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def make_sound(self):
        raise NotImplementedError("Subclass must implement abstract method")

class Dog(Animal):
    def make_sound(self):
        return "Woof!"

class Cat(Animal):
    def make_sound(self):
        return "Meow!"

# Example usage:
dog = Dog("Buddy", "Canine")
cat = Cat("Whiskers", "Feline")

print(dog.make_sound())  # Output: Woof!
print(cat.make_sound())  # Output: Meow!

Top 10 Spring Boot Projects You Must Try: Detailed Guide

4. Encapsulation: Student

Task: Define a class Student that has private attributes for name and grade. Provide public methods to get and set the values of these attributes.

class Student:
    def __init__(self, name, grade):
        self.__name = name  # Private attribute
        self.__grade = grade  # Private attribute

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

    def get_grade(self):
        return self.__grade

    def set_grade(self, grade):
        self.__grade = grade

# Example usage:
student = Student("Alice", "A")
print(student.get_name())  # Output: Alice
print(student.get_grade())  # Output: A

student.set_name("Bob")
student.set_grade("B")
print(student.get_name())  # Output: Bob
print(student.get_grade())  # Output: B
  • Complete Python Course : Click here
  • Free Notes :- Click here
  • New Project :-https://www.youtube.com/@Decodeit2
  • Java Projects – Click here

5. Object Interaction: Library and Book

Task: Create a class Library with methods to add_book() and remove_book() using the Book class. Implement a method list_books() that prints all the books currently in the library.

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def display_info(self):
        print(f"Title: {self.title}, Author: {self.author}, Pages: {self.pages}")

class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.append(book)
        print(f"Added book: {book.title}")

    def remove_book(self, title):
        for book in self.books:
            if book.title == title:
                self.books.remove(book)
                print(f"Removed book: {title}")
                return
        print(f"Book not found: {title}")

    def list_books(self):
        if not self.books:
            print("No books in the library.")
        else:
            print("Books in the library:")
            for book in self.books:
                book.display_info()

# Example usage:
library = Library()
book1 = Book("1984", "George Orwell", 328)
book2 = Book("To Kill a Mockingbird", "Harper Lee", 281)

library.add_book(book1)
library.add_book(book2)
library.list_books()
# Output:
# Added book: 1984
# Added book: To Kill a Mockingbird
# Books in the library:
# Title: 1984, Author: George Orwell, Pages: 328
# Title: To Kill a Mockingbird, Author: Harper Lee, Pages: 281

library.remove_book("1984")
library.list_books()
# Output:
# Removed book: 1984
# Books in the library:
# Title: To Kill a Mockingbird, Author: Harper Lee, Pages: 281

These examples cover several key aspects of OOP:

  • Class Creation: How to define classes and methods.
  • Inheritance: Creating subclasses and overriding methods.
  • Encapsulation: Using private attributes and public methods to control access.
  • Object Interaction: Working with multiple classes and their instances to perform complex tasks.

Checkout these below links !

  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL
Post Views: 693
code Snippets Tags:classes in python, inheritance in python, object oriented programming, object oriented programming in python, object oriented programming python, object-oriented programming, object-oriented programming in python, objects in python, oops in python, Programming, Python, python class, python classes, python object oriented programming, python object oriented programming advanced, python objects, python oop, python programming, python programming tutorial, Python Tutorial

Post navigation

Previous Post: Chapter 9: Object-Oriented Programming OOP in Python
Next Post: Create a Snake Game in Java

More Related Articles

Tic-Tac-Toe Game in Python with Source Code - Tic-Tac-Toe Tic-Tac-Toe Game in Python with Source Code code Snippets
Movie Management System in Python with Source Code - Movie Management System in Python with Source Code Movie Management System in Python with Source Code code Snippets
Create Address Book in Python with Source Code - Create Address Book Create Address Book in Python with Source Code code Snippets

Leave a Reply Cancel reply

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

You may also like

  1. Supply Chain Management PHP and CSS
  2. Book Library in JavaScript With Free Code
  3. F1 Race Road Game in Python Free Source Code
  4. Student Management System in Python With Free Code
  5. Create Address Book in Python with Source Code
  6. Contact Management in Python with Source Code

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,614)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,215)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,867)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme