• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

Object-Oriented Programming in Java: Concepts and Examples

Document preview thumbnail
Preview 2 out of 5 pages

This document explains Object-Oriented Programming (OOP) in Java, covering key concepts like classes and objects, inheritance, encapsulation, polymorphism, and abstraction. Learn how to apply OOP principles in Java with practical examples. Perfect for second-year Computer Science students.

Content preview

Object-Oriented Programming (OOP) concepts
in Java

1. Class and Object
 Class: A blueprint for creating objects (a particular data structure), defining
fields (attributes) and methods (functions) that describe the behavior of the
objects.
 Object: An instance of a class. It has its own unique state and can call
methods defined in the class.

Example:

class Car {
String model;
int year;

void start() {
System.out.println("The car is starting.");
}
}

public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.model = "Toyota";
myCar.year = 2020;
myCar.start();
}
}

, 2. Encapsulation
 The process of wrapping data (variables) and code (methods) together as a
single unit.
 It hides the internal state of the object from the outside world and only
allows access through public methods (getters and setters).
 Access modifiers: private, protected, public.

Example:

class Account {
private double balance;

public double getBalance() {
return balance;
}

public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
}
}
}


3. Inheritance
 A mechanism where one class acquires the properties and behaviors
(methods) of another class.
 It allows for code reuse and method overriding.
 A class that inherits from another is called a subclass, and the class it
inherits from is the superclass.

Document information

Uploaded on
January 23, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown
$7.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions