top of page

Abstraction in Java

  • Writer: Keshari Abeysinghe
    Keshari Abeysinghe
  • Mar 23, 2020
  • 2 min read

Updated: Nov 30, 2020

Abstraction in any programming language works in many ways. It can be seen from creating subroutines to defining interfaces for making low-level language calls.Interfaces allows you to abstract the implementation completely while abstract classes allow partial abstraction as well.


Abstract class, uses the abstract keyword to declare the class .It has both abstract and non - abstract methods to hide the implementations and show the functionalities only.It cannot be instantiated.Abstract class can have constructors ,static methods , final attributes also.Abstract method uses abstract keyword to declare the method.


Following example contains both abstract methods and non abstract methods and how to implement them using another class .



abstract class School
{
void listen(){
System.out.println("Student is listening to the teacher");
}
abstract void teach();
}
class  Teacher extends School()
{
void teach(){
System.out.println("Teacher is teaching");
}
public static void main(String args[])
{
School scl=new Teacher();
scl.teach();
}
}

Expected output

Student is listening to the teacher
Teacher is teaching

Interfaces , are declared using interface keyword.It is a blue print of a class.It provides total abstraction.It means all the methods are abstract methods(no body),all the fields are public, static and final by default.Interfaces has is -a relationship and they are used to multiple inheritance in java. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. The implementation part is hidden by the user who uses the interface.


A class extends another class, an interface extends another interface, but a class implements an interface.

interface School{
void teach();
}

class Teacher implements School{
public void teach()
{
System.out.println("Teacher teach English");
}
public static void main(String[] args)
{
Teacher teaching=new Teacher();
teaching.teach();
}
}

Multiple inheritance

Classes implements multiple interfaces and interface also extends multiple interfaces.It is known as multiple inheritance,As the ambiguity in classes it is not supports to multiple inheritance for classes.However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.


Following example shows how a class implements several interfaces

interface Teacher{ 
void teach();
}

interface Tutor{
void teach();
}

class School implements Teacher,Student{
public void teach(){
System.out.println("Hellow")
}

public static void main(String[] args){
School sch=new School();
sch.teach();
}
}

Expected Output

Hello

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

Example for how extends multiple interfaces by the interface

interface Teacher{
void teach();
}

interface Student{
void student();
}

interface  Principal extends Teacher,Student(){
void plan();
}

class Scheduling implements Principal(){
 public void plan(){
 System.out.println("Plan the  time table");
 }

public void student(){
System.out.println("Study well");
}

public void teach(){
system.out.println("teaching English");
}

public static void main(string[] args){
Scheduling schedule=new Scheduling();
 schedule.plan();
 schedule.teach();
 schedule.student();
}
}
}

Expected output


Plan the  time table
Teaching English
Study well

In the Java 8 and 9 version have several changes with interfaces ,they will discussing on upcoming topics ..


Happy Coding!

Recent Posts

See All
Inheritance

Inheritance is a mechanism in which one class acquires the property of another class.For example, a child inherits the traits of his/her...

 
 
 
Aggregation in java

In Java, aggregation represents HAS-A relationship, which means when a class contains reference of another class known to have...

 
 
 
Polymorphism

Polymorphism in Java occurs when there are one or more classes or objects related to each other by inheritance. In other words, it is the...

 
 
 

Comments


Subscribe Form

Thanks for submitting!

©2020 by Quick Code. Proudly created with Wix.com

bottom of page