Object-oriented programming is a programming concept that uses objects to represent real-world concepts and entities. Java is one of the most popular programming languages supporting OOP principles.
Here are some of the principles of OOP in Java, along with code examples:
Encapsulation:
- Encapsulation is the process of hiding the details of the implementation of an object.
Only the necessary functionality is exposed. - In java encapsulation, this can be achieved through private, public, and protected modifiers.
public class Car {
private String make;
// getter methods
public String getMake() {
return make;
}
// setter methods
public void setMake(String make) {
this.make = make;
}
}
Inheritance:
- Inheritance is the process where one class acquires properties and methods of another class.
- It is inheriting all traits from the parent to the child.
- The class which inherits properties is a subclass, aka a child class.
- Achieved using the "extends" keyword.
class parent{
method1{}
method2{}
}
Class child extends parent{
method3{}
}
child class = method1 from parent, method2 from parent and method3 from its own.
Polymorphism:
- The ability of an object to take different forms is known as polymorphism.
- This can be achieved by method overloading and method overriding.
Method Overloading: When more than one method with the same name but different parameters are declared, this is called method overloading.
package week4.day2;
public class boom {
public int add(int x, int y) {
return x + y;
}
public int add(int x, int y, int z) {
return x + y + z;
}
public static void main(String[] args) {
boom b = new boom();
System.out.println(b.add(1, 1));
System.out.println(b.add(1, 1, 1));
}
}
Output:
2
3
Method Overriding: If the child class has the same method as the parent class, it is called method overriding in JavaJava.
package week4.day2;
public class boom {
public int add(int x, int y) {
return x + y;
}
public int add(int x, int y, int z) {
return x + y + z;
}
public static void main(String[] args) {
boom b = new boom();
System.out.println(b.add(1, 1));
System.out.println(b.add(1, 1, 1));
}
}
package week4.day2;
Public class BoomTwo extends boom {
@Override
public int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Abstraction:
- In Java, abstraction is the process of hiding the implementation details of a class and then exposing only the essential features to the outside world.
- Abstraction is done using abstract classes and interfaces.
- Using abstraction, you can create classes and interfaces that define a contract for their behavior without specifying the exact implementation.
- Interfaces are similar to abstract classes in that they cannot be instantiated, but they can only contain abstract methods, constant fields, and default methods (methods with a default implementation).
Abstract Example:
public abstract class Shape {
public abstract void draw();
}
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Rectangle extends Shape {
public void draw() {
System.out.println("Drawing a rectangle.");
}
}
- We have an abstract class, Shape, with an abstract method, draw().
- The Circle and Rectangle classes extend the Shape class and provide their implementation of the draw() method.