Oops with java for BCA
Oops with java for BCA
CODE :
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
Objects
Objects are instances of classes. They represent real-world
entities with a unique identity, state, and behavior.
Code:
2. Encapsulation
Encapsulation is the concept of wrapping data (variables) and
code (methods) together as a single unit. In encapsulation,
the variables of a class will be hidden from other classes, and
can only be accessed through the methods of their current
class. This is also known as data hiding.
Benefits:
• Protects the internal state of an object from unwanted
or harmful external modification.
• Reduces complexity by hiding the internal
implementation details.
• Improves maintainability and flexibility
CODE:
// Constructor
public BankAccount(String accountNumber, double
balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Superclass
class Vehicle {
private String brand;
// Subclass
class Car extends Vehicle {
private int numberOfDoors;
4. Polymorphism
Polymorphism allows objects to be treated as instances of
their parent class rather than their actual class. There are two
types of polymorphism in Java:
• Compile-time Polymorphism (Method Overloading):
When multiple methods have the same name but
different parameters.
• Runtime Polymorphism (Method Overriding): When a
subclass provides a specific implementation of a method
already defined in its superclass.
Benefits:
• Simplifies code maintenance and readability.
• Promotes flexibility and integration.
Method Overloading (Compile-time Polymorphism):
CODE :
class MathOperations {
public int add(int a, int b) {
return a + b;
}
CODE:
class Animal {
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
CODE:
abstract class Shape {
abstract void draw();
}
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}
6. Association
Association represents a relationship between two
classes. It can be one-to-one, one-to-many, many-to-
one, or many-to-many. Association is a broader
concept that encompasses both aggregation and
composition.
Example:
CODE:
class Author {
String name;
class Book {
String title;
Author author;
Example:
CODE:
class Engine {
String type;
class Car {
String model;
Engine engine;
class Heart {
public void pump() {
System.out.println("Heart is pumping blood");
}
}
class Human {
private final Heart heart;
public Human() {
this.heart = new Heart();
}
1. Encapsulation
Encapsulation is the bundling of data (attributes)
and methods (functions) that operate on the data
into a single unit, typically a class. It restricts
direct access to some of the object's components,
which can help prevent accidental interference
and misuse of the data.
Key Points:
• Data Hiding: Internal state of the object is
hidden from the outside. Only the object's
methods can interact with its internal state.
• Access Modifiers: Control access to the class's
fields and methods. Common modifiers
include private, protected, and public.
Example:
CODE:
public class BankAccount {
private String accountNumber;
private double balance;
// Constructor
public BankAccount(String accountNumber, double
balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
In the BankAccount class, the fields accountNumber and balance are encapsulated and
can only be accessed or modified through the class's public methods.
2. Inheritance
Inheritance is a mechanism where one class (child or
subclass) inherits the attributes and methods of another class
(parent or superclass). This promotes code reuse and
establishes a natural hierarchical relationship between
classes.
Key Points:
• Reusability: Allows the child class to reuse the fields and
methods of the parent class.
• Extensibility: Enables the child class to extend or
override the behaviors of the parent class.
Example:
CODE:
// Superclass
class Vehicle {
private String brand;
// Subclass
class Car extends Vehicle {
private int numberOfDoors;
In this example, the Car class inherits properties and methods from the Vehicle class.
3. Polymorphism
Polymorphism allows objects to be treated as instances of
their parent class rather than their actual class. This enables a
single interface to represent different underlying forms (data
types). There are two types of polymorphism:
• Compile-time Polymorphism (Method Overloading):
Methods with the same name but different parameters.
• Runtime Polymorphism (Method Overriding): A
subclass provides a specific implementation of a method
that is already defined in its superclass.
Key Points:
• Flexibility: Different classes can be treated through the
same interface.
• Interchangeability: Objects of different classes can be
treated as objects of a common superclass.
class MathOperations {
public int add(int a, int b) {
return a + b;
}
In the above examples, method overloading allows different methods to share the same name but
differ in parameters, while method overriding allows a subclass to provide a specific implementation
of a method that is already defined in its superclass.
4. Abstraction
Abstraction is the concept of hiding the complex
implementation details and showing only the essential
features of the object. It is achieved using abstract classes
and interfaces.
Key Points:
• Simplification: Reduces complexity by hiding the
implementation details.
• Focus on Essentials: Allows developers to focus on what
an object does rather than how it does it.
Example of Abstract Class:
CODE:
abstract class Shape {
abstract void draw();
}
Example of Interface:
CODE:
interface Animal {
void eat();
void makeSound();
}
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}
In these examples, the Shape class and Animal interface provide abstract methods that
must be implemented by any subclass or implementing class, thus hiding the details and
exposing only the necessary functionalities.
Conclusion