Activity12-17-24
Activity12-17-24
12-17-24
Example:
CopyReplit
public class Car {
String color;
String model;
}
Methods: These are functions defined within a class that describe the behaviors of the object.
Example:
CopyReplit
public void drive() {
System.out.println("Driving the car");
}
Constructors: Special methods used to initialize objects of a class.
Example:
CopyReplit
public Car(String color, String model) {
this.color = color;
this.model = model;
}
Example:
CopyReplit
Car myCar = new Car("Red", "Toyota");
CopyReplit
public class Car {
String color;
String model;
9. What is the purpose of getter and setter methods? How do they balance accessibility and security?
Getter and setter methods provide controlled access to an object's fields. Getters allow reading the
values, while setters allow modifying the values, typically including validation logic. This approach strikes
a balance, allowing controlled access while protecting the internal state.
Example:
CopyReplit
public class Car {
private String color;
Example:
CopyReplit
public void drive() {
System.out.println("Driving the car");
}
Static Methods: These methods belong to the class itself and can be called without an instance. They
cannot access instance variables.
Example:
CopyReplit
public static void honk() {
System.out.println("Honking");
}
Example:
CopyReplit
public void add(int a, int b) {
System.out.println(a + b);
}
Inheritance: This is an "is-a" relationship where a class inherits properties and methods from another
class.
13. How can objects of one class be used within another class? Provide an example.
You can create an instance of one class inside another class to utilize its methods and properties.
Example:
CopyReplit
public class Engine {
public void start() {
System.out.println("Engine started");
}
}
Example:
CopyReplit
public class Vehicle {
public void drive() {
System.out.println("Driving");
}
}
Example:
CopyReplit
public class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
Example:
CopyReplit
public class Vehicle {
public Vehicle() {
System.out.println("Vehicle created");
}
}
Object-Oriented Principles
17. How do classes and objects demonstrate the principles of abstraction, encapsulation, inheritance,
and polymorphism?
Abstraction: Classes abstract complex behavior into simple interfaces. For example, a Car class abstracts
the details of various car models into a unified interface.
Encapsulation: Classes bundle data and methods and restrict access through access modifiers and
getters/setters, protecting the internal state.
Inheritance: Classes can inherit fields and methods from other classes, promoting code reuse.
Polymorphism: Methods can be overridden in subclasses, allowing for dynamic method resolution at
runtime.
18. Why is object-oriented programming beneficial compared to procedural programming?
Object-oriented programming (OOP) promotes greater modularity, code reuse, and maintainability
compared to procedural programming. OOP encapsulates data and behavior together, aligns better with
real-world modeling, offers better code organization through classes, and simplifies complex systems
through abstraction.
Real-World Applications
19. How can classes and objects be used to model real-world entities, such as a library system, a
student database, or an e-commerce website?
In a library system, classes could represent Book, Member, and Library. Each class would have fields and
methods that relate to its real-world counterpart, such as checking out books or managing members.
Example:
CopyReplit
public class Book {
String title;
String author;
20. Reflect on a project where you used classes and objects. What challenges did you face, and how
did you solve them?
In a recent project developing a simple e-commerce application, I created multiple classes such as
Product, Cart, and User. One challenge was managing the relationships between these classes,
particularly with the Cart class interacting with multiple Product instances. I resolved this by
implementing appropriate methods to add and remove items in the cart and ensuring that all products
were represented as objects, enabling a clear and manageable structure for operations and data flow.