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

OOP Concepts Detailed Java Laptop Example

This document provides an overview of fundamental Object-Oriented Programming (OOP) concepts in Java, including Encapsulation, Abstraction, Inheritance, and Polymorphism, with detailed examples. It also explains the roles of JDK, JRE, and JVM in Java application development and execution. Each concept is illustrated with code snippets to demonstrate practical implementation.

Uploaded by

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

OOP Concepts Detailed Java Laptop Example

This document provides an overview of fundamental Object-Oriented Programming (OOP) concepts in Java, including Encapsulation, Abstraction, Inheritance, and Polymorphism, with detailed examples. It also explains the roles of JDK, JRE, and JVM in Java application development and execution. Each concept is illustrated with code snippets to demonstrate practical implementation.

Uploaded by

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

OOP Concepts in Java with Detailed Examples

This document explains the fundamental OOP concepts in Java using real-world examples.
We'll explore Encapsulation, Abstraction, Inheritance, and Polymorphism with examples,
along with information on JDK, JRE, and JVM.

1. Encapsulation
Encapsulation is the concept of wrapping data and methods into a single unit, called a class.
It restricts access to certain components to ensure controlled modification and data
integrity. In Java, encapsulation is achieved using private variables and public getter and
setter methods.

Example:

class Laptop {
private String brand;
private int ramSize;
private double price;

public Laptop(String brand, int ramSize, double price) {


this.brand = brand;
this.ramSize = ramSize;
this.price = price;
}

public String getBrand() { return brand; }


public void setBrand(String brand) { this.brand = brand; }
public int getRamSize() { return ramSize; }
public void setRamSize(int ramSize) { this.ramSize = ramSize; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }

public void displayLaptopInfo() {


System.out.println("Brand: " + brand + ", RAM: " + ramSize + "GB, Price: $" + price);
}
}

2. Abstraction
Abstraction is the process of hiding the implementation details and showing only the
necessary features of an object. It helps in reducing complexity. Java provides abstraction
through abstract classes and interfaces.
Example:

interface ElectronicDevice {
void powerOn();
void powerOff();
}

class Laptop implements ElectronicDevice {


private String brand;
private int ramSize;
private double price;

public Laptop(String brand, int ramSize, double price) {


this.brand = brand;
this.ramSize = ramSize;
this.price = price;
}

public void powerOn() { System.out.println("Laptop is powering on..."); }


public void powerOff() { System.out.println("Laptop is shutting down..."); }

public void displayLaptopInfo() {


System.out.println("Brand: " + brand + ", RAM: " + ramSize + "GB, Price: $" + price);
}
}

3. Inheritance
Inheritance allows one class to inherit fields and methods from another class, promoting
code reuse. In Java, inheritance is achieved using the 'extends' keyword.

Example:

class GamingLaptop extends Laptop {


private String gpuModel;

public GamingLaptop(String brand, int ramSize, double price, String gpuModel) {


super(brand, ramSize, price); // Call superclass constructor
this.gpuModel = gpuModel;
}

public void displayGamingLaptopInfo() {


displayLaptopInfo();
System.out.println("GPU Model: " + gpuModel);
}
}

4. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. It
has two types: Method Overloading and Method Overriding.

4.1 Method Overloading (Compile-Time Polymorphism):

Method Overloading is achieved by defining multiple methods with the same name but
different parameters within a class.

Example:

class Laptop {
private String brand;
private int ramSize;
private double price;

public Laptop(String brand, int ramSize, double price) {


this.brand = brand;
this.ramSize = ramSize;
this.price = price;
}

public void displayInfo() {


System.out.println("Brand: " + brand + ", RAM: " + ramSize + "GB, Price: $" + price);
}

// Overloaded method
public void displayInfo(String feature) {
System.out.println("Brand: " + brand + ", RAM: " + ramSize + "GB, Price: $" + price + ",
Feature: " + feature);
}

public void displayInfo(double discount) {


System.out.println("Brand: " + brand + ", RAM: " + ramSize + "GB, Price after discount:
$" + (price - discount));
}
}
4.2 Method Overriding (Runtime Polymorphism):

Method Overriding occurs when a subclass provides a specific implementation of a method


already defined in its superclass.

Example:

class GamingLaptop extends Laptop {


private String gpuModel;

public GamingLaptop(String brand, int ramSize, double price, String gpuModel) {


super(brand, ramSize, price);
this.gpuModel = gpuModel;
}

// Overriding displayLaptopInfo method


@Override
public void displayLaptopInfo() {
super.displayLaptopInfo();
System.out.println("GPU Model: " + gpuModel);
}
}

JDK, JRE, JVM


1. JDK (Java Development Kit): JDK contains tools required for developing Java applications,
such as the compiler and debugger. It includes the JRE for executing Java programs.
- Example: When writing Java code, you use JDK tools to compile it into bytecode.
2. JRE (Java Runtime Environment): JRE provides the libraries and JVM needed to run Java
applications. It does not include development tools.
- Example: A user with the JRE can run a compiled Java application but cannot develop it.
3. JVM (Java Virtual Machine): JVM interprets Java bytecode and converts it to machine code
for execution. It allows Java to be platform-independent.
- Example: JVM runs a compiled bytecode file (.class) on any supported platform.

You might also like