Chapter 4 Object-Oriented Programming (OOP) Concepts
Chapter 4 Object-Oriented Programming (OOP) Concepts
Page 1 of 36
PROPOSED BY AZOBOU CEDRIC
Creating Objects
To create an object, you use the new keyword followed by the class constructor. The constructor
initializes the fields of the object.
public class Main {
public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car("Toyota", "Corolla", 2020);
Car car2 = new Car("Honda", "Civic", 2021);
Page 2 of 36
PROPOSED BY AZOBOU CEDRIC
Page 3 of 36
PROPOSED BY AZOBOU CEDRIC
Page 4 of 36
PROPOSED BY AZOBOU CEDRIC
Instance Variables
An instance variable is a variable that is declared inside a class but outside any method,
constructor, or block. Each object created from the class has its own copy of the instance
variable. This means that instance variables represent the state of an object.
Characteristics of Instance Variables:
• They are associated with an object.
• Every object has its own set of instance variables.
• They can have different values for different objects created from the same class.
• Instance variables can have access modifiers like private, public, protected, or
package-private (default).
Example of Instance Variables:
public class Car {
// Instance variables
Page 5 of 36
PROPOSED BY AZOBOU CEDRIC
String make;
String model;
int year;
Page 6 of 36
PROPOSED BY AZOBOU CEDRIC
• Instance Variables: make, model, and year are instance variables. Each car object will
have its own values for these variables.
• When you create car1 and car2, each object has its own copy of the instance variables.
2. Methods
A method in Java is a block of code that performs a specific task. Methods can be
associated with the behavior of an object or can operate on the data contained in the instance
variables. Methods can access instance variables, and they define what an object can do.
Types of Methods:
• Instance Methods: Methods that operate on instance variables of an object.
• Static Methods: Methods that are associated with the class itself and do not operate on
instance variables.
Characteristics of Methods:
• Instance methods belong to an object and can access instance variables of the object.
• Static methods belong to the class and can access only static variables or call other
static methods.
Example of Instance Methods:
public class Car {
// Instance variables
String make;
String model;
int year;
// Constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Instance method
public void displayDetails() {
System.out.println("Car Make: " + make);
Page 7 of 36
PROPOSED BY AZOBOU CEDRIC
Page 8 of 36
PROPOSED BY AZOBOU CEDRIC
// Constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
carCount++; // Increment the car count each time a new car is created
}
// Instance method
public void displayDetails() {
Page 9 of 36
PROPOSED BY AZOBOU CEDRIC
Page 10 of 36
PROPOSED BY AZOBOU CEDRIC
Page 11 of 36
PROPOSED BY AZOBOU CEDRIC
// Default constructor
public Car() {
// Default initialization
this.make = "Unknown";
this.model = "Unknown";
this.year = 0;
}
Page 12 of 36
PROPOSED BY AZOBOU CEDRIC
// Parameterized constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
Page 13 of 36
PROPOSED BY AZOBOU CEDRIC
Constructor Overloading
Java allows constructor overloading, which means you can define multiple constructors with
different parameter lists. This provides flexibility in creating objects with different initialization
options.
Example of Constructor Overloading:
public class Car {
// Instance variables
String make;
String model;
Page 14 of 36
PROPOSED BY AZOBOU CEDRIC
int year;
// Default constructor
public Car() {
this.make = "Unknown";
this.model = "Unknown";
this.year = 0;
}
// Parameterized constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
Page 15 of 36
PROPOSED BY AZOBOU CEDRIC
Key Points:
• Default Constructor: A constructor with no parameters. If you don't explicitly define any
constructor, Java provides a default no-argument constructor.
• Parameterized Constructor: A constructor that accepts parameters and initializes the
object's fields with provided values.
• Constructor Overloading: You can define multiple constructors in the same class with
different parameters.
Summary:
• Constructors are special methods used to initialize objects in Java.
• Default constructors provide default values for instance variables, while
parameterized constructors allow you to set specific values when creating an object.
• Java also supports constructor overloading, where multiple constructors with different
parameters can be defined in a class to offer more flexibility in object creation.
Page 16 of 36
PROPOSED BY AZOBOU CEDRIC
Encapsulation
In Java, encapsulation is one of the fundamental principles of Object-Oriented
Programming (OOP). It refers to the practice of bundling the data (variables) and the methods
(functions) that operate on the data into a single unit, known as a class. Encapsulation also
involves restricting direct access to some of the object's components and providing controlled
access through getter and setter methods.
Access modifiers are used to implement encapsulation in Java. Access modifiers are used to
specify the level of access control for classes, methods, and variables. They determine how and
where a class, method, or variable can be accessed within the program. Additionally, getter and
setter methods are used to provide controlled access to the instance variables of a class.
public
➢ The public access modifier allows the class, method, or variable to be accessed from
anywhere, inside or outside the package.
➢ It provides the broadest level of access.
private
➢ The private access modifier restricts access to the class, method, or variable only within
the same class.
➢ Private variables and methods cannot be accessed outside the class, even by objects
created from that class.
protected
➢ The protected access modifier allows access to the class, method, or variable within the
same package or by subclasses (even if they are in different packages).
➢ Protected members can be accessed in the same package and by subclass objects.
Default (Package-Private)
➢ If no access modifier is specified, the default access is applied. This means the class,
method, or variable is accessible only within the same package.
➢ It has package-private access, which is more restrictive than protected but less
restrictive than private.
Examples of Access Modifiers: Let’s define a class Person with various access modifiers
applied to its attributes and methods.
public class Person {
// Public variable - can be accessed anywhere
public String name;
// Private variable - can only be accessed within this class
Page 17 of 36
PROPOSED BY AZOBOU CEDRIC
Page 18 of 36
PROPOSED BY AZOBOU CEDRIC
Page 19 of 36
PROPOSED BY AZOBOU CEDRIC
Page 20 of 36
PROPOSED BY AZOBOU CEDRIC
Page 21 of 36
PROPOSED BY AZOBOU CEDRIC
Page 22 of 36
PROPOSED BY AZOBOU CEDRIC
Page 23 of 36
PROPOSED BY AZOBOU CEDRIC
Key Points:
➢ extends Keyword: Used to establish an inheritance relationship where a subclass
inherits from a superclass.
➢ super Keyword: Used to refer to the superclass’s constructor or methods. It helps in
calling the superclass’s constructor to initialize the inherited properties.
➢ Subclass Methods: The subclass can access and modify the fields and methods of the
superclass, and it can also define its own fields and methods.
Page 24 of 36
PROPOSED BY AZOBOU CEDRIC
Overriding Methods:
If the subclass wants to provide a specific implementation of a method that is already
defined in the superclass, it can override the method. The @Override annotation is used to
indicate method overriding.
// Superclass (Parent class)
class Vehicle {
public void startEngine() {
System.out.println("The vehicle's engine is starting...");
}
}
Page 26 of 36
PROPOSED BY AZOBOU CEDRIC
System.out.println("Bark");
}
}
• At runtime, the actual method that gets invoked is determined by the type of the object
that the reference points to, not the type of the reference variable.
Output:
Bark
Meow
• For the first object (a Dog), the sound() method of the Dog class is called, which
outputs "Bark".
• For the second object (a Cat), the sound() method of the Cat class is called,
which outputs "Meow".
➢ This is an example of runtime polymorphism, because the method to be invoked (Dog's
sound() or Cat's sound()) is determined dynamically at runtime based on the actual
object type, not the reference type.
Important Characteristics of Dynamic Method Dispatch:
➢ Runtime Polymorphism occurs when a subclass overrides a method of the superclass,
and the method call is resolved at runtime.
➢ The method signature (name, return type, and parameters) must be the same in both
the superclass and the subclass for overriding to occur.
➢ Method Overriding is necessary for dynamic dispatch. The overridden method in the
subclass must have the same method signature as in the superclass.
Page 28 of 36
PROPOSED BY AZOBOU CEDRIC
class Animal {
public void sound() {
System.out.println("Animal sound");
}
}
Page 29 of 36
PROPOSED BY AZOBOU CEDRIC
Page 30 of 36
PROPOSED BY AZOBOU CEDRIC
Abstraction in Java
Abstraction is the concept of hiding the internal implementation details of an object and
exposing only the essential features. It helps in reducing complexity and allows focusing on what
the object does rather than how it does it.
Types of Abstraction:
1. Abstract Classes: A class that cannot be instantiated directly and can contain both
abstract (without implementation) and non-abstract (with implementation) methods.
2. Interfaces: A contract that defines a set of abstract methods, which must be
implemented by any class that chooses to implement the interface.
Key Points:
➢ An abstract class can have both abstract methods (without implementation) and
concrete methods (with implementation).
➢ An interface can only have abstract methods (prior to Java 8, but this has been
enhanced in later versions).
Abstract Class in Java
An abstract class is a class that cannot be instantiated on its own and must be
subclassed. It can contain abstract methods (methods without implementation) and non-
abstract methods (methods with implementation).
Page 31 of 36
PROPOSED BY AZOBOU CEDRIC
@Override
public void sound() {
System.out.println(name + " barks.");
}
}
@Override
public void sound() {
System.out.println(name + " meows.");
}
}
Page 32 of 36
PROPOSED BY AZOBOU CEDRIC
Interface in Java
An interface in Java is a reference type, similar to a class, that can contain only abstract
methods (methods without implementation), though starting from Java 8, interfaces can have
default and static methods with implementations.
Key Characteristics of Interfaces:
➢ Abstract Methods: All methods in an interface are abstract by default (before Java 8).
➢ Default Methods: From Java 8, interfaces can have default methods with a body
(implementation).
➢ Static Methods: From Java 8, interfaces can have static methods.
➢ Multiple Implementations: A class can implement multiple interfaces, overcoming
Java's limitation of single inheritance for classes.
Syntax of an Interface:
Page 33 of 36
PROPOSED BY AZOBOU CEDRIC
interface Animal {
// Abstract method (no implementation)
void sound();
Page 34 of 36
PROPOSED BY AZOBOU CEDRIC
Page 35 of 36
PROPOSED BY AZOBOU CEDRIC
o You have a common base class with some shared behavior (i.e., concrete
methods) and some abstract behavior (i.e., abstract methods).
o You want to provide a partial implementation of functionality, and subclasses
can inherit and override some of these methods.
• Use an Interface when:
o You want to define a contract that multiple classes should adhere to, regardless
of their place in the class hierarchy.
o You need to implement multiple inheritance (a class can implement multiple
interfaces).
As a conclusion:
• Abstraction helps you hide the complexity and show only the essential features. It can
be achieved using abstract classes and interfaces.
• Abstract classes allow a mix of abstract and concrete methods, whereas interfaces
provide a way to specify a contract that implementing classes must follow.
• Interfaces are often used when a class can implement more than one contract, whereas
abstract classes are used when common behavior is shared among classes.
Page 36 of 36