Java OOP (Object Oriented Programming)
Object Oriented Programming (OOP) concepts revolve around the concept of objects.
In this tutorial, we will go through different OOP Concepts with detailed examples, and links to corresponding tutorials.
Java Inheritance
Inheritance allows an object of a class to own the variables and methods of another class. In Java, Inheritance is realized using the keyword extends.
More about Java Inheritance.
In the following example, we have two classes: A and B. A is super class and B is subclass. Or in other words class B inherits class A.
A.java
public class A {
public void printMsg() {
System.out.println("Hello World!");
}
}
B.java
public class B extends A {
public static void main(String[] args) {
B b = new B();
b.printMsg();
}
}
Output
Hello World!
Java Polymorphism
Polymorphism lets methods to occur with same name but different forms.
More about Java Polymorphism.
Java Overloading
A class can have methods with same name but different set of arguments.
More about Java Method Overloading.
In the following example, we have two methods with the same name printMsg, but the number of parameters in their definition are different. This is call Method Overloading in Java.
B.java
public class B {
public static void main(String[] args) {
B b = new B();
b.printMsg();
b.printMsg("Good Morning!");
}
public void printMsg() {
System.out.println("Hello User!");
}
public void printMsg(String msg) {
System.out.println(msg);
}
}
Output
Hello User!
Good Morning!
Java Overriding
Child class can have a method with same name and same set of arguments as that of super class. This is called overriding in Java.
More about Java Method Overriding.
In the following example, sub class B overrides the method inherited from class A. This is called Method Overriding in Java.
A.java
public class A {
public void printMsg() {
System.out.println("Hello World!");
}
}
B.java
public class B extends A {
public static void main(String[] args) {
A a = new B();
a.printMsg();
}
public void printMsg() {
System.out.println("Hello User!");
}
}
Output
Hello User!
Java Abstraction
Abstract class contains one or more abstract methods. And any class that extends this abstract class must implement these abstract methods.
More about Java Abstraction.
A.java
public abstract class A {
public abstract void printMsg();
}
B.java
public class B extends A {
public static void main(String[] args) {
B b = new B();
b.printMsg();
}
public void printMsg() {
System.out.println("Hello User!");
}
}
Output
Hello User!
Java Interface
Java Interface enforces implementations of methods in classes that implement the interface.
More about Java Interfaces.
In the following example, class A has an empty method printMsg(). Class B is implementing the interface A, and therefore class A has to implement all the methods of class A. We have overridden the printMsg() method in class B.
A.java
public interface A {
public void printMsg();
}
B.java
public class B implements A {
public static void main(String[] args) {
A a = new B();
a.printMsg();
}
@Override
public void printMsg() {
System.out.println("Hello User!");
}
}
Output
Hello User!
Java Encapsulation
Encapsulation wraps data in an object, and gives access to the properties of the object through getter and setter methods.
More about Java Encapsulation.
In the following example, class A has a property msg. msg could be set only when the object is created via its constructor. And you can only read the property but can never change it, because we have not defined a set method to update the property value.
A.java
public class A {
private String msg;
public A(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
B.java
public class B {
public static void main(String[] args) {
A a = new A("Hello World!");
System.out.println(a.getMsg());
}
}
Output
Hello World!
Conclusion
In this Java Tutorial, we learned different Object Oriented Programming Concepts in Java.