Skip to main content

Interface Vs Abstract Class After Java 8

Interface Vs Abstract Class After Java 8
With the introduction of concrete methods (default and static methods) to interfaces from Java 8, the gap between interface and abstract class has been reduced significantly. Now both can have concrete methods as well as abstract methods. But, still there exist some minute differences between them. In this article, we will try to list down the differences between interface Vs abstract class after Java 8.

Differences Between Interface And Abstract Class After Java 8 :

1) Fields

Interface fields are public, static and final by default. Interfaces still don’t support non-static and non-final variables. Interfaces can only have public, static and final variables. On the other hand, abstract class can have static as well as non-static and final as well as non-final variables. They also support private and protected variables along with public variables.

2) Methods

After Java 8, an interface can have default and static methods along with abstract methods. Interfaces don’t support final methods. But, abstract classes support final as well as non-final methods and static as well as non-static methods along with abstract methods.
Also note that, only interfaces can have default methods. Abstract classes can’t have default methods.

3) Constructors

Interfaces can’t have constructors. Abstract classes can have any number of constructors.

4) Member’s Accessibility

All members of interfaces are public by default. Interfaces don’t support private and protected members. But, abstract classes support all type of members – private, protected and public members.

5) Multiple Inheritance

A class can extend only one abstract class, but can implement multiple interfaces. Thus, a class can inherit multiple properties from multiple sources only through interfaces, not through abstract classes.

Interface Vs Abstract Class After Java 8 :

The below table and program summarizes the similarities and differences between interface and abstract class after Java 8.

Interface Vs Abstract Class After Java 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
interface anyInterface
{
    int i = 10;            //By default, interface fields are public, static and final
     
    void abstractMethod();          //Interface can have abstract method
     
    default void defaultMethod()
    {
        System.out.println("Interface can have default method");
    }
     
    static void staticMethod()
    {
        System.out.println("Interface can have static method");
    }
     
    //No constructors in an interface
     
    //No non-static and non-final variables in an interface
     
    //No private fields and methods in an interface
     
    //No protected fields and methods in an interface
     
    //No final methods in an interface
}
abstract class anyAbstractClass
{
    private int a;          //Abstract class can have private field
     
    protected int b;        //Abstract class can have protected field
     
    public int c;           //Abstract class can have public field
     
    static int d;           //Abstract class can have static field
     
    final int e = 10;       //Abstract class can have final field
     
    int f;                  //Abstract class can have non-static and non-final field
     
    public anyAbstractClass()
    {
        System.out.println("Abstract class can have constructors");
    }
      
    abstract void abstractmethod();    //Abstract class can have abstract method
     
    private static void staticMethod()
    {
        System.out.println("Abstract class can have private and static method");
    }
     
    public void nonStaticMethod()
    {
        System.out.println("Abstract class can have public and non-static method");
    }
     
    protected void protectedMethod()
    {
        System.out.println("Abstract class can have protected method");
    }
     
    final void finalMethod()
    {
        System.out.println("Abstract class can have final method");
    }
     
    //No default method in an abstract class
}

Comments

Popular posts from this blog

gRPC with Java : Build Fast & Scalable Modern API & Microservices using Protocol Buffers

gRPC Java Master Class : Build Fast & Scalable Modern API for your Microservice using gRPC Protocol Buffers gRPC is a revolutionary and modern way to define and write APIs for your microservices. The days of REST, JSON and Swagger are over! Now writing an API is easy, simple, fast and efficient. gRPC is created by Google and Square, is an official CNCF project (like Docker and Kubernetes) and is now used by the biggest tech companies such as Netflix, CoreOS, CockRoachDB, and so on! gRPC is very popular and has over 15,000 stars on GitHub (2 times what Kafka has!). I am convinced that gRPC is the FUTURE for writing API for microservices so I want to give you a chance to learn about it TODAY. Amongst the advantage of gRPC: 1) All your APIs and messages are simply defined using Protocol Buffers 2) All your server and client code for any programming language gets generated automatically for free! Saves you hours of programming 3) Data is compact and serialised 4) API ...

What is Big Data ?

What is Big Data ? It is now time to answer an important question – What is Big Data? Big data, as defined by Wikipedia, is this: “Big data is a broad term for  data sets  so large or complex that traditional  data processing  applications are inadequate. Challenges include  analysis , capture,  data curation , search,  sharing ,  storage , transfer ,  visualization ,  querying  and  information privacy . The term often refers simply to the use of  predictive analytics  or certain other advanced methods to extract value from data, and seldom to a particular size of data set.” In simple terms, Big Data is data that has the 3 characteristics that we mentioned in the last section – • It is big – typically in terabytes or even petabytes • It is varied – it could be a traditional database, it could be video data, log data, text data or even voice data • It keeps increasing as new data keeps flowing in This kin...

GraphQL - A Short Intro

Why GraphQL is the future of APIs Since the beginning of the web, developing APIs has been a difficult task for developers. The way we develop our APIs must evolve with time so that we can always build good, intuitive and well-designed APIs. In the last few years, GraphQL has been growing in popularity among developers. A lot of companies have started adopting this technology to build their APIs. GraphQL is a query language developed by Facebook in 2012 and released publicly in 2015. It has been gaining a lot of traction. It has been adopted by a lot of big companies such as Spotify, Facebook, GitHub, NYTimes, Netflix, Walmart, and so on. In this series of tutorials, we’re going to examine GraphQL, understand what it is, and see what features make this query language so intuitive and easy to use. So, let’s get started by examining the problems with REST, and how GraphQL solves them. We will also find out why companies have been building their APIs with GraphQL, and ...