Object-Oriented Programming
Creating an Application in Java
🚀 Introduction
Have you ever used a mobile app like a calculator or a game? These apps are created using programming languages. One of the most popular languages is Java. It is used all around the world to build software, mobile apps, and games.
In this blog, we will learn about Object-Oriented Programming (OOP) using Java and how you can create a simple app with it. Don't worry—it's going to be very easy and fun!
🎭 Understanding Object-Oriented Programming (OOP)
OOP stands for Object-Oriented Programming. It is a way of writing code by thinking in terms of real-life objects.
Let's say you want to make a program for cars. In OOP, you don't just write code — you create a Car object with parts like wheels, engine, and methods like start(), stop(), etc.
OOP helps make our code:
Organized
Easy to reuse
Simple to understand
Think of a TV remote. It has buttons (functions) and settings (data). Together, they work as a unit—just like an object in programming.
🔧 Core Concepts of OOP
🏗️ Class and Object
A class is like a blueprint. Just like a blueprint of a house shows what it will look like, a class defines what an object will contain.
Example:
class Student {
String name;
int age;
}
String name;
int age;
}
An object is the real thing created using the class.
Student s1 = new Student();
💊 Encapsulation
Encapsulation means hiding the data inside the class and allowing access only through methods.
Example:
class Student {
private int age;
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
}
private int age;
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
}
👨👩👧👦 Inheritance
Inheritance means one class can inherit the properties of another.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
void eat() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
🎭 Polymorphism
Polymorphism means one name, many forms.
Example:
class Shape {
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
🚗 Abstraction
Abstraction means showing only important things and hiding details.
Example:
abstract class Vehicle {
abstract void start();
}
class Bike extends Vehicle {
void start() {
System.out.println("Bike started");
}
}
abstract void start();
}
class Bike extends Vehicle {
void start() {
System.out.println("Bike started");
}
}
⚙️ Setting Up Java
1. Download Java JDK from oracle.com
2. Install it step by step (just like any software)
3. You can use Notepad or IDE like Eclipse or IntelliJ IDEA
Write this in a file named Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Run it using command:
javac Hello.java
java Hello
java Hello
💻 Creating a Simple Java Application
Let's create a small app called Student Info App. It will store a student's name, age, and grade and show this info when needed.
Step-by-step Code:
class Student {
String name;
int age;
String grade;
void setInfo(String n, int a, String g) {
name = n;
age = a;
grade = g;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
}
}
public class StudentApp {
public static void main(String[] args) {
Student s1 = new Student();
s1.setInfo("Jatasya", 14, "A");
s1.displayInfo();
}
}
String name;
int age;
String grade;
void setInfo(String n, int a, String g) {
name = n;
age = a;
grade = g;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
}
}
public class StudentApp {
public static void main(String[] args) {
Student s1 = new Student();
s1.setInfo("Jatasya", 14, "A");
s1.displayInfo();
}
}
Compile:
javac StudentApp.java
Run:
java StudentApp
Output:
Name: Jatasya
Age: 14
Grade: A
Age: 14
Grade: A
🌟 Benefits of Learning OOP and Java
🎯
Makes programming easier and logical
🏗️
Helps you build apps that are organized
🏢
Java is used by companies like Google, Amazon, etc.
📱
It is good for Android apps
🎓
Helps you learn other languages like Python or C++ easily
🎉 Conclusion
Object-Oriented Programming with Java is like building things with Lego blocks—you create small parts (objects) and connect them together. It makes your code clean, reusable, and powerful. With simple practice, anyone can learn to build real apps using OOP.
❓ FAQs
1. What is OOP in simple words?
OOP is a way of programming using real-world things like cars, animals, or students. Each thing is treated as an object.
2. Can I learn Java easily?
Yes! If you practice daily and start with small programs, you can learn Java without any difficulty.
3. What apps are made using Java?
Java is used to make Android apps, games, banking software, and much more.
4. Is OOP useful for games?
Absolutely! Games have characters, scores, and rules—all these can be written using OOP.
5. What is the difference between class and object?
A class is a design or plan, and an object is the real version created using that plan.
💝 Thank You for Reading!
Please don't forget to leave a review/comments and follow us.
0 Comments
If you have any doubts, Please let me know