Day 1: Introduction to Java

icon picker
Day 8: More OOP Concepts in Java

Welcome back to our Java programming journey! In the previous days, we've covered the fundamental concepts of Object-Oriented Programming (OOP) in Java, including classes, objects, inheritance, and encapsulation. Today, we're diving deeper into OOP by exploring the concepts of polymorphism, abstraction, interfaces, packages, and access modifiers.

Polymorphism

Polymorphism is one of the pillars of OOP and allows objects of different classes to be treated as objects of a common superclass. This concept simplifies code and enhances flexibility. In Java, polymorphism is achieved through method overriding and method overloading. The word “poly” means many and “morphs” means forms, So it means many forms.
image.png

Compile-Time Polymorphism

It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading

Runtime Polymorphism

It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

Method Overriding

javaCopy code
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}

public class PolymorphismExample {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks
myAnimal = new Cat();
myAnimal.sound(); // Outputs: Cat meows
}
}

Method Overloading

javaCopy code
class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {
return a + b;
}
}

public class PolymorphismExample2 {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(1, 2)); // Outputs: 3
System.out.println(calc.add(1.5, 2.5)); // Outputs: 4.0
}
}

Advantages of Polymorphism in Java

Increases code reusability by allowing objects of different classes to be treated as objects of a common class.
Improves readability and maintainability of code by reducing the amount of code that needs to be written and maintained.
Supports dynamic binding, enabling the correct method to be called at runtime, based on the actual class of the object.
Enables objects to be treated as a single type, making it easier to write generic code that can handle objects of different types.

Disadvantages of Polymorphism in Java

Can make it more difficult to understand the behavior of an object, especially if the code is complex.
This may lead to performance issues, as polymorphic behavior may require additional computations at runtime.

Abstraction

Abstraction is the process of simplifying complex reality by modeling classes based on the essential properties and behaviors they share. In Java, we achieve abstraction through abstract classes and methods.
The abstract keyword is a non-access modifier, used for classes and methods: Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body.

Abstract Classes and Methods

javaCopy code
abstract class Shape {
abstract double calculateArea();
}

class Circle extends Shape {
double radius;

Circle(double radius) {
this.radius = radius;
}

@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}

public class AbstractionExample {
public static void main(String[] args) {
Shape circle = new Circle(5);
System.out.println("Circle Area: " + circle.calculateArea());
}
}

Interfaces

An interface defines a contract that a class must adhere to by implementing its methods. In Java, multiple inheritance is achieved through interfaces.
An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations
javaCopy code
interface Drawable {
void draw();
}

class Circle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}

class Rectangle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a rectangle");
}
}

public class InterfaceExample {
public static void main(String[] args) {
Drawable circle = new Circle();
Drawable rectangle = new Rectangle();

circle.draw(); // Outputs: Drawing a circle
rectangle.draw(); // Outputs: Drawing a rectangle
}
}

Packages and Access Modifiers

Packages help organize classes and avoid naming conflicts. Access modifiers control the visibility and accessibility of classes, methods, and variables within and outside the package.

Package and Access Modifier Example

javaCopy code
package com.example.myapp;

public class MyClass {
public int publicVar = 1;
protected int protectedVar = 2;
int defaultVar = 3;
private int privateVar = 4;
}

Exercise Questions:
Explain the concept of polymorphism with suitable examples.
How is abstraction implemented in Java? Provide an example.
What is the purpose of interfaces in Java? Provide an example of their usage.
Describe the role of packages and access modifiers in Java. Give examples to illustrate their usage.
Exercise Answers:
Polymorphism in Java allows objects of different classes to be treated as objects of a common superclass. It can be achieved through method overriding and method overloading. Example code is provided in the blog.
Abstraction in Java is implemented using abstract classes and methods. Abstract classes define a contract for subclasses to follow. An example is provided in the blog using the Shape class and its subclass Circle.
Interfaces in Java define a contract that classes must implement by providing method implementations. They enable multiple inheritance and help achieve abstraction. An example is provided in the blog using the Drawable interface.
Packages in Java help organize classes into logical groups and avoid naming conflicts. Access modifiers control the visibility of classes, methods, and variables. Public, protected, default (package-private), and private access modifiers are used to control access within and outside packages. An example with access modifiers is given in the blog.
Today, we've delved into more advanced OOP concepts in Java. Practice these concepts and their code examples to solidify your understanding. In the next blog, we'll explore more Java topics to enhance your programming skills. Happy coding!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.