Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
Object-Oriented Programming Terminologies
Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and code that manipulates the data. In this article, we will learn about basic terminologies of OOP in Java, including classes, objects, inheritance and others using examples.
1. Classes
In object-oriented programming, a class is a blueprint for creating objects. It defines the state and behavior of an object, and serves as a template for creating objects of the same type.
A class is defined using the class
keyword, followed by the name of the class. The class definition includes variables and methods, which represent the state and behavior of the class.
Here is an example of a simple class in Java:
public class Person {
// properties of the Person class
String name;
int age;
// methods of the Person class
void setName(String n) {
name = n;
}
void setAge(int a) {
age = a;
}
}
In this example, the Person class has two properties: name and age. It also has two methods: setName() and setAge(), which are used to set the values of the name and age properties.
2. Objects
An object is an instance of a class. It contains the state and behavior of the class, and is created using the new keyword and a constructor method.
Here is an example of creating an object of the Person class:
Person person1 = new Person();
In this example, we create a new object of the Person class, and assign it to the person1 variable.
Once we have created an object, we can access its properties and methods using the dot (.) operator. For example:
person1.name = "John";
System.out.println(person1.name); // Prints John.
3. Inheritance
Inheritance is a way for one class to inherit the properties and methods of another class. In Java, a class can extend another class, which means it can inherit the fields and methods of the superclass. The class that extends the superclass is called the subclass, and the superclass is called the parent class. Inheritance allows us to create a hierarchy of classes, where a subclass can inherit the characteristics of its parent class and also add its own unique characteristics. This allows for code reuse, as we can create a parent class with shared characteristics and then create subclasses that inherit those characteristics and add their own specific behavior.
Here is a simple example of inheritance in Java:
class Animal {
public void move() {
System.out.println("Animals can move.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dogs can bark.");
}
}
public class TestInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.bark();
d.move();
}
}
In this example, the Dog class is derived from the Animal class. The Dog class inherits the move() method from the Animal class and also has its own bark() method. When we create an object of the Dog class and call the move() method, the move() method from the Animal class is called.
4. Polymorphism
Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is achieved through method overloading and method overriding.
- Method overloading refers to the ability of a class to have multiple methods with the same name, but with different parameters. This allows the class to perform the same action in different ways, depending on the parameters provided.
- Method overriding refers to the ability of a subclass to override, or replace, a method of its superclass. This allows the subclass to provide its own implementation of the method, while still maintaining the same method signature as the superclass.
Here is an example of method overloading in the Calculator class:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public String add(String a, String b) {
return a + b;
}
}
In this example, the add() method is overloaded to accept different data types as parameters. The first method takes two int values, the second method takes two double values, and the third method takes two String values.
5. Abstraction
Abstraction is the process of hiding the implementation details of a class and only exposing the necessary functions to the user. This allows the user to interact with the class without worrying about how the class performs its functions.
Abstraction is achieved in Java through the use of abstract classes and interfaces.
- An abstract class is a class that cannot be instantiated and must be extended by a subclass. An abstract class can contain both abstract and concrete methods. Abstract methods are methods that have a declaration, but no implementation. This means that the subclass must implement the abstract method.
- An interface is a collection of abstract methods that must be implemented by any class that implements the interface. An interface can also contain constant variables.
Here is an example of an abstract class in the Shape
hierarchy:
public abstract class Shape {
public abstract void draw();
public void fillColor(String color) {
System.out.println("Filling shape with " + color + " color.");
}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Square extends Shape {
@Override
public void draw() {
System.out.println("Drawing a square.");
}
}
6. Encapsulation
Encapsulation is the process of wrapping data and methods that operate on that data within a single unit, or object. This allows the object to protect its data from outside access or modification and encourages modular programming.
Encapsulation is achieved in Java through the use of access modifiers. Access modifiers are keywords that are used to set the accessibility of a class, method, or variable.
There are four access modifiers in Java: public
, private
, protected
, and default
.
Here is an example of encapsulation in java using the BankAccount class:
public class BankAccount {
private int accountNumber;
private String accountHolder;
private double balance;
public BankAccount(int accountNumber, String accountHolder, double balance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountHolder() {
return accountHolder;
}
public void setAccountHolder(String accountHolder) {
this.accountHolder = accountHolder;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}