Introduction to Object-Oriented Programming (OOP)
1.1 OOP Concepts:
- Objects: Objects are the basic units of OOP. They are instances of classes and represent entities with attributes (data) and behaviors (methods). For example, a
Car
object might have attributes likecolor
,model
, andyear
, and behaviors likedrive()
andbrake()
. - Class: A class is a blueprint for creating objects. It defines the attributes and behaviors that the objects created from the class will have. For example:
public class Car {
String color;
String model;
int year;
void drive() {
System.out.println("The car is driving.");
}
void brake() {
System.out.println("The car is braking.");
}
}
- Encapsulation: Encapsulation is the concept of wrapping data (variables) and methods (functions) together as a single unit. It restricts direct access to some of the object’s components, which can prevent the accidental modification of data. For example:
public class Car {
private String color;
private String model;
private int year;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
- Abstraction: Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. For example, when you drive a car, you don’t need to understand the internal workings of the engine.
- Inheritance: Inheritance is a mechanism where one class inherits the properties and behaviors of another class. This allows for code reuse and the creation of a hierarchical relationship between classes. For example:
public class Vehicle {
String brand;
void honk() {
System.out.println("Honk! Honk!");
}
}
public class Car extends Vehicle {
String modelName = "Mustang";
}
- Polymorphism: Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The most common use of polymorphism is when a parent class reference is used to refer to a child class object. For example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
}
}
- Message Passing: In OOP, objects communicate with each other by sending messages (calling methods). For example:
public class Sender {
public void sendMessage() {
System.out.println("Message sent!");
}
}
public class Receiver {
public static void main(String[] args) {
Sender sender = new Sender();
sender.sendMessage();
}
}
1.2 Java Virtual Machine (JVM):
- The JVM is a virtual machine that enables Java programs to run on any device or operating system. It converts Java bytecode into machine code. The JVM is platform-independent, which means that the same Java program can run on any device that has the JVM installed.
1.3 Basic Programming Constructs:
- Variables: Variables are containers for storing data values. For example:
int age = 25;
String name = "Ajink";
- Data Types: Data types specify the type of data that a variable can hold. Common data types in Java include
int
(integer),float
(floating point),char
(character),boolean
(true/false), andString
(text). - Operators: Operators are symbols that perform operations on variables and values. For example, arithmetic operators (
+
,-
,*
,/
), comparison operators (==
,!=
,>
,<
), and logical operators (&&
,||
). - Unsigned Right Shift Operator: The unsigned right shift operator (
>>>
) shifts the bits of a number to the right and fills the leftmost bits with zero. For example:
int a = -1;
System.out.println(a >>> 1); // Outputs 2147483647
- Expressions: An expression is a combination of variables, operators, and values that evaluates to a single value. For example:
int result = 5 + 3;
- Branching: Branching allows the program to take different paths based on conditions. Common branching statements include
if
,else
,switch
. For example:
int number = 10;
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
- Looping: Looping allows a set of statements to be executed repeatedly based on a condition. Common looping statements include
for
,while
, anddo-while
. For example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}