Java Oops Concept with Example
Java is a powerful object-oriented programming language that has become one of the most popular programming languages in the world. It is an excellent language for developing large-scale, complex applications, and its popularity is due in part to its use of object-oriented programming (OOP) principles. OOP is a programming paradigm that uses objects and their interactions to design and implement complex software systems. In this article, we will discuss the key concepts of OOP in Java.
- Objects:
In OOP, everything is an object. An object is an instance of a class, which contains data and the code to manipulate that data. Objects have properties (data) and methods (functions). In Java, you create an object using the "new" keyword.
- Classes:
A class is a blueprint for creating objects. It defines the properties and methods of the objects. A class can be thought of as a template or a blueprint for an object. In Java, a class is defined using the "class" keyword.
- Encapsulation:
Encapsulation is the process of hiding the internal details of an object from the outside world. In Java, this is achieved through access modifiers such as public, private, and protected. Public methods and properties can be accessed from outside the class, while private methods and properties are hidden from the outside world.
- Inheritance:
Inheritance is a mechanism in which one object acquires the properties and methods of another object. The object that is being inherited from is called the parent or super class, while the object that is inheriting is called the child or sub-class. In Java, you can use the "extends" keyword to inherit from a parent class.
- Polymorphism:
Polymorphism means the ability of an object to take on many forms. In Java, polymorphism is achieved through method overloading and method overriding. Method overloading is the process of defining multiple methods with the same name but with different parameters. Method overriding is the process of defining a method in a child class that is already defined in the parent class.
- Abstraction:
Abstraction is the process of hiding the implementation details of an object and showing only the necessary details to the user. In Java, you can achieve abstraction through abstract classes and interfaces. Abstract classes are classes that cannot be instantiated and are used to define the common properties of its sub-classes. Interfaces are similar to abstract classes but are used to define a set of methods that a class must implement.
In conclusion, these are the key concepts of OOPs in Java. Understanding these concepts is essential for writing efficient and effective Java code. By using these principles, you can create code that is modular, easy to maintain, and can be reused in different projects.
In conclusion, these are the key concepts of OOPs in Java. Understanding these concepts is essential for writing efficient and effective Java code. By using these principles, you can create code that is modular, easy to maintain, and can be reused in different projects.
Java Oops Concept Examples
- Objects:
Let's say we want to create a class called "Person" which has properties like name, age, and gender. We can create an object of this class as follows:
class Person {
String name;
int age;
String gender;
}
Person person1 = new Person();
person1.name = "John";
person1.age = 30;
person1.gender = "Male";
- Classes:
We can create a class called "Car" which has properties like make, model, and year. We can define methods within the class to perform actions on the car, such as accelerating, braking, and changing gears.
class Car {
String make;
String model;
int year;
void accelerate() {
// code to increase speed of the car
}
void brake() {
// code to decrease speed of the car
}
void changeGear(int gear) {
// code to change the gear of the car
}
}
Car car1 = new Car();
car1.make = "Toyota";
car1.model = "Camry";
car1.year = 2020;
car1.accelerate();
car1.changeGear(3);
- Encapsulation:
We can create a class called "BankAccount" which has properties like account number, account holder name, and balance. We can use access modifiers to encapsulate the balance property and only allow access to it through methods within the class.
class BankAccount {
private int accountNumber;
private String accountHolderName;
private double balance;
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
}
BankAccount account1 = new BankAccount();
account1.deposit(1000);
account1.withdraw(500);
double balance = account1.getBalance();
- Inheritance:
We can create a class called "Animal" which has properties like name and species. We can then create sub-classes like "Cat" and "Dog" which inherit properties and methods from the "Animal" class.
class Animal {
String name;
String species;
}
class Cat extends Animal {
void meow() {
// code to make the cat meow
}
}
class Dog extends Animal {
void bark() {
// code to make the dog bark
}
}
Cat cat1 = new Cat();
cat1.name = "Whiskers";
cat1.species = "Felis catus";
cat1.meow();
- Polymorphism:
We can create a class called "Shape" which has a method called "calculateArea" which calculates the area of the shape. We can then create sub-classes like "Rectangle" and "Circle" which override the "calculateArea" method to calculate the area of the specific shape.
class Shape {
public double calculateArea() {
// code to calculate area of a shape
}
}
class Rectangle extends Shape {
public double calculateArea() {
// code to calculate area of a rectangle
}
}
class Circle extends Shape {
public double calculateArea() {
// code to calculate area of a circle
}
}
Shape shape1 = new Rectangle();
double area1 = shape1.calculateArea();
Shape shape2 = new Circle();
double area2 = shape2.calculateArea();
- Abstraction:
We can create an interface called "Shape" which has a method called "calculateArea". We can then create classes like "Rectangle" and "Circle" which implement the "Shape" interface and provide their own implementation of the "calculateArea" method.
interface Shape {
double calculateArea();
}
class Rectangle implements Shape {
double length;
double width;
public double calculateArea() {
return length * width;
}
}
class Circle implements Shape {
double radius;
public double calculateArea() {
return Math.PI * radius * radius;
}
}
Shape shape1 = new Rectangle();
((Rectangle)shape1).length = 5;
((Rectangle)shape1).width = 10;
double area1 = shape1.calculateArea();
Shape shape2 = new Circle();
((Circle)shape2).radius = 5;
double area2 = shape2.calculateArea();
In this example, the "Shape" interface provides an abstraction for calculating the area of a shape. The "Rectangle" and "Circle" classes implement this interface and provide their own implementation of the "calculate Area" method. By using the "Shape" interface, we can write code that works with any class that implements it, without having to know the specific implementation details. This allows for greater flexibility and modularity in our code.