What is Object Oreinted Programming Flashcards

(11 cards)

1
Q

What is object oriented programming?

A

Object Oriented Programming is a style of programing based on organize code by using objects, these objects are instances of cclass that group data and functions that operate on them together. Object oriented programming is used to model real-world things as objects to make programs easier to understand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 4 principles of Object Oriented programming?

A
  • Abstaction
  • Polymorphism
  • Inheritance
  • Encapsulation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Abstaction and how is it implemented in Java?

A

Abstaction:
Abstration is the concept of hinding complex implentation details, while exposing only essentail features to users, this is to help reduce complexity and allow programs to focus on what an object does rather than now the object does it.

How abstraction is implemented in Java:
Any time a method or attribute of an object is used, that is abstraction as the programmer or users dont worry about the back ground implementation of the methods. but coding wise abstaction can be implemented in two ways:

  1. using an abstact with astact methods/ methods with no body/implemetation:

abstract class Animal {
abstract void sound(); // abstract method (no body)

void sleep() {
    System.out.println("Sleeping...");
} }

class Dog extends Animal {
void sound() {
System.out.println(“Bark”);
}
}

  1. Using Interfaces: Interfaces define methods with not implemetations that classes implement instaed:

interface Vehicle {
void move();
}

class Car implements Vehicle {
public void move() {
System.out.println(“Car is moving”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an abstact class?

A

Is a class in which objects cannot be directly created from but instead is there for other classes to inherit from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is polymorphism and how is it implemented in Java?

A

Polymorphism, means something has many forms. It is a concept that allows the same actions or methods to behave differently depending on the object that is performing/calling it.

There are two ways in which polymorphism is implemented in java:

Compiletime Ploymorphism: This is when multiple methods of a class have the same name but differ in the number or data types of thier parametters, it is called compile time polyformasm because it is at the time of compilation that the java compiler determines which function is to be called based on the number and datatypes of parammeters.

e.g:
class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {
    return a + b;
} }
  1. Runtime Polymorphism:
    This is when a child class provides thier own implementation of a method already defined in the parent class, it is called run time polymorphisma because the JVM decides which methods to run at run time depending on the object calling the method.

class Animal {
void sound() {
System.out.println(“Some animal sound”);
}
}

class Dog extends Animal {
void sound() {
System.out.println(“Bark”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is another name for compile-time polymorphism?

A

method overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is another name for run-time polymorphism?

A

method overriding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Inheritance and how is it implemented in java?

A

It is a concept and feature that allos a class to obtain all the properties and methods of another class.

Inheritance is implemented in ava using the extends key word for example:

// Parent class (superclass)
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}

// Child class (subclass)
class Dog extends Animal {
void bark() {
System.out.println(“Dog barks”);
}
}

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark(); // Defined in Dog
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is encapsulation and how is it implemented in Java?

A

Encapsulation is the concept of cobining data and operations operating on that data into a single unit, in the case of java cobining variables or properties to gether with the functions/methods operating on them into a class, this also allows implementation details to be hidden.

Exapsulation is implemed in java with the help of access modifies, for example making the properties of a class private but making the getters and setters to those properties public e.g

public class Person {
// Step 1: Private variables
private String name;
private int age;

// Step 2: Public getter and setter methods

public String getName() {
    return name;
}

public void setName(String newName) {
    name = newName;
}

public int getAge() {
    return age;
}

public void setAge(int newAge) {
    if (newAge >= 0) {
        age = newAge;
    }
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantantages of Object oriented progring.

A
  • Code Re-usability: OOP allows you to reuse existing code throught inheritance, allowing new classes to be created based on existing ones without re-writing code

e.g
class Animal {
void eat() {
System.out.println(“Eating…”);
}
}

class Dog extends Animal {
void bark() {
System.out.println(“Barking…”);
}
}

  1. Data Security through Encapsulation:
    OOP protects data by hiding it inside classes and controlling access through methods.

e.g

class Person {
private String name;

public String getName() {
    return name;
}

public void setName(String n) {
    name = n;
} }
  1. Easy to maintain and modify code:
    With OOP, code is organized in classes, making it easier to maintain or update one part without affecting others.
  2. Improved Code Organization:
    OOP lets you group related variables and functions into classes, making the program structure clearer and more logical. e.g

class BankAccount {
private double balance;

void deposit(double amount) {
    balance += amount;
}

void withdraw(double amount) {
    balance -= amount;
} }
  1. Supports Real-World Modeling:
    OOP allows you to model real-world entities like students, cars, and bank accounts naturally using classes and objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given a scenario where you need to design a system for a Library Management Software using Java, justify your choice of data structure and design pattern to ensure efficiency and scalability.

A

To build an efficient and scalable Library Management System in Java, I would use the MVC (Model-View-Controller) design pattern along with essential data structures.

The MVC pattern separates concerns:
* The Model handles data (like Book and User classes), e.g

class Book {
private String id, title;
private boolean isAvailable;
// getters and setters
}

  • The View handles the user interface, with say java swing.
  • The Controller handles business logic (like adding or finding books). e.g

class LibraryController {
private Map<String, Book> bookCatalog = new HashMap<>();

public void addBook(Book book) {
    bookCatalog.put(book.getId(), book);
}

public Book findBook(String id) {
    return bookCatalog.get(id);
} }

This separation improves maintainability and allows different developers to work on different layers.

For data structures:
* HashMap is used for fast lookup of books or users by ID,

HashMap<String, Book> bookCatalog = new HashMap<>();
bookCatalog.put(“B001”, new Book(“Java Basics”, “John Doe”));

  • ArrayList for managing lists of borrowed books e.g

ArrayList<Book> issuedBooks = new ArrayList<>();</Book>

Together, MVC and these structures ensure a clean, scalable, and efficient system design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly