Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Week 6 T+L 8 Object-Oriented Programming

These are three documents on programming basic concepts.

Uploaded by

golatosami09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week 6 T+L 8 Object-Oriented Programming

These are three documents on programming basic concepts.

Uploaded by

golatosami09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Method Overloading (Compile-

time polymorphism)

class Vehicle {

void travel() {

System.out.println("The vehicle is
traveling...");

class Car extends Vehicle {

@Override

class Car {
void travel() {

// Private field to restrict direct access


System.out.println("You are traveling by
car on the road.");
private String travels; // represents the
type of travel (e.g., "road", "off-road")
}
String mode;
// Constructor to initialize travels
}
int speed;
public Car(String travels) {
class Bike extends Vehicle {
// Default constructor
this.travels = travels;
@Override
Vehicle() {
} Polymorphism lets us perform void travel() {
a single action in multiple ways. mode = "unknown";
// Public getter for travels
It’s achieved in two main ways System.out.println("You are traveling by
bike on the road.");
public String getTravels() { in Java: speed = 0;

} }
return travels;

} // Constructor with one parameter


}

class Airplane extends Vehicle { Vehicle(String mode) {


// Public setter for travels
Example of Polymorphism with
Practice Program
Method Overriding: @Override this.mode = mode;
public void setTravels(String travels) {

void travel() { speed = 0;


this.travels = travels;

System.out.println("You are traveling by }


Encapsulation wraps data and } airplane in the sky.");
methods into a single unit ( // Constructor with two parameters
// Method to display car travel type }
class) and protects data from
outside access. Private public void displayInfo() {
Vehicle(String mode, int speed) {
}
instance variables and public
this.mode = mode;
getter and setter methods System.out.println("Travels by: " + public class Main {
travels);
enforce encapsulation by this.speed = speed;
controlling how data is public static void main(String[] args) {
}
accessed and modified. Constructor Overloading allows }
// Create an array of Vehicle references
} a class to have multiple
// Display vehicle information
Vehicle[] vehicles = new Vehicle[3];
constructors with different
public class Main { parameter lists, providing
void displayInfo() {
// Store different vehicle objects flexibility in object initialization.
public static void main(String[] args) {
System.out.println("Traveling by " +
vehicles[0] = new Car(); mode + " at a speed of " + speed + " km/h.");
// Create a Car object
Method Overriding (Runtime
Car myCar = new Car("road");
polymorphism) vehicles[1] = new Bike(); }

vehicles[2] = new Airplane(); }


// Display initial travel type

// Loop through each vehicle and call public class Main {


System.out.println("Original Travel travel()
Type:");
public static void main(String[] args) {
for (Vehicle v : vehicles) {
myCar.displayInfo();
OOP Principles // Using different constructors
v.travel(); // Polymorphism in action
// Update travel type using setter
Vehicle defaultVehicle = new Vehicle();
Encapsulation }
myCar.setTravels("off-road");
Vehicle roadVehicle = new Vehicle("
} road");
Inheritance System.out.println("\nUpdated Travel
Type:");
} Vehicle airVehicle = new Vehicle("air",
900);
Polymorphism myCar.displayInfo();
Parent Class Vehicle: This class has a travel() method with generic functionality.
// Displaying information
} Subclasses (Car, Bike, Airplane):
Teacher MIRHAZAR KHAN
Method Overloading Each subclass overrides the travel() method to provide specific behavior. defaultVehicle.displayInfo();
}
For example, Car specifies traveling on the road, while Airplane specifies traveling in
Constructor Overloading the sky. roadVehicle.displayInfo();
Explanation: Here, name and Polymorphism in Action:
airVehicle.displayInfo();
age are private, accessible only The Vehicle array stores objects of different subclasses.
Passing and Returning Objects
through public methods When the travel() method is called on each object, the overridden method in the
}
getName(), setName(), respective subclass is executed.
Access Specifiers getAge(), and setAge(). }

Week 6 T+L 8
Object-Oriented Topics 1.OOP Principles 2. Encapsulation 3. Inheritance 4. Polymorphism 5. Method Overloading 6. Constructor Overloading

Programming
Bundling of data (attributes) class Vehicle { class Vehicle {

and actions (methods) within a


Encapsulation: // Overloaded travel method with no
class, restricting access to void travel() { arguments
some components.
System.out.println("you traveled..."); void travel() {

Establishing a relationship
System.out.println("The vehicle is
between classes, where one } traveling...");
Inheritance: class (child) inherits properties
The four main principles of and behaviors from another } }

OOP help structure code in a class (parent).


// Overloaded travel method with one
modular, reusable, and class Car extends Vehicle { argument
understandable way: Allowing objects to be treated
as instances of their parent void travel() { void travel(String mode) {
Polymorphism:
class, enabling a single method
System.out.println("The vehicle is
to perform different tasks. System.out.println("you traveled by road");
traveling by " + mode + ".");

Hiding complex implementation Inheritance allows a class to derive properties and } }


details, exposing only behaviors from another class. The class that
Abstraction: necessary functionality (usually inherits is called the child class, and the class it } // Overloaded travel method with two
arguments
achieved through abstract inherits from is the parent class. Use the extends
classes or interfaces). keyword to create an inheritance relationship. public class Main { void travel(String mode, int speed) {

Example
public static void main(String[] args) { System.out.println("The vehicle is
traveling by " + mode + " at a speed of " +
speed + " km/h.");
Vehicle myVehicle = new Vehicle();
}

myVehicle.travel(); // Calls Vehicle's travel method


}
Main Class

Car myCar = new Car(); public class Main {

myCar.travel(); // Calls Car's overridden travel method public static void main(String[] args) {

Vehicle myVehicle = new Vehicle();


}
// Calling overloaded travel methods
}
myVehicle.travel(); // No argument

myVehicle.travel("road"); // One
argument

myVehicle.travel("air", 800); // Two


arguments

You might also like