JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Abstraction in Java

The meaning of the word “Abstraction”, in general words, is the process of working with ideas rather than their implementation.

For example, consider the example of TV remote. You know that when you press any button in remote, some function is applied on television e.g. change the channel, change the volume level etc. You are not need to know how internally remote works.

An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.

Abstraction defines an object in terms of its properties (attributes), behavior (methods), and interfaces (means of communicating with other objects).

There are two ways to achieve Abstraction in java:

1. Abstract class

2. Interface

Abstract Class

An abstract class is a class that is declared with abstract keyword. It may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

But, if a class has at least one abstract method, then the class must be declared abstract.

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract

It can have constructors and static methods also.

It can have final methods which will force the subclass not to change the body of the method.

Example of abstract class:

abstract class Math{} 

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

Thats means An abstract method contains a method signature, but no method body.

Example of abstract method:

abstract void display();//no method body

Example of asbstract class and abstract method

abstract class Math
                {
                    abstract void display();
                }
                
                class Addition extends Math
{
    void display()
    {
        int a = 3, b = 2;
        System.out.println("A + B = "+(a+b));
    }
} 

class Multiplication extends Math
{
    void display()
    {
        int a = 3, b = 2;
        System.out.println("A * B = "+(a*b));
    }
}
class Division extends Math
{
    void display()
    {
        int a = 3, b = 2;
        System.out.println("A / B = "+(a/b));
    }
} 

class ModuleDivision extends Math
{
    void display()
    {
        int a = 3, b = 2;
        System.out.println("A % B = "+(a%b));
    }
}
class Abstract
{
    public static void main(String args[])
    {
        Addition a = new Addition();
        a.display();
        Multiplication m = new Multiplication();
        m.display();
        Division d = new Division();
        d.display();
        ModuleDivision md = new ModuleDivision();
        md.display();
    }
}

Output:

A + B = 5
A * B = 6
A / B = 1
A % B = 1