What the difference between Abstract Class and Interface in Java

What the difference between Abstract Class and Interface in Java

In this article, we will learn about Abstract class and Interface in the Java programming language.

Introduction

In this article, we will learn about Abstract class and Interface in the Java programming language. Abstract class and interface are used for abstraction. Both Abstract class and the interface can not be initiated.

Abstract class

In Java, when we use abstract keyword with any class, this class is called an abstract class. An abstract class can have variables, abstract method, a method body, constructor and main () method also. Here are some important points about  Abstract class.

  • An abstract class can have abstract methods and non-abstract methods.
  • If a class has an abstract method, this class must be an abstract class.
  • If we want to use an abstract class, it needs to be extended and its method implemented.
  • If we extend an abstract class, we have to provide an implementation for all the abstract methods in it.
  • We can’t instantiate an abstract class.

The complete program of an abstract class is listed below.

abstract class AbstractClass {    
    abstract void display();    
}    
    
public class AbstractClassexample extends AbstractClass {    
    void display() {    
        System.out.println("Abstract class example");    
    }    
    
    public static void main(String args[]) {    
        AbstractClass p = new AbstractClassexample();    
        p.display();    
    }    
}     

The above program generates the following output.

Interface

An interface in Java is similar to a class and we can say that it is a blueprint of a class. Here are some important points about Abstract class.

  • The interface is mainly used to achieve full abstraction in Java.
  • The interface has static constants and abstract method body only and no method body.
  • Interface supports multiple inheritance in Java.
  • An interface shows the IS-A relationship also.
  • Interface can’t be instantiated, as in the case of an abstract class.

In Java, the compiler by default adds public and abstract keywords before the interface method and public, static and final keyword before the variables. It means that all the variables of an interface are public, static, final and methods are public and abstract.

In Java, Multiple inheritances are not supported through the class. It is possible through an interface because of the interface methods implementation provided by the other class. Hence, there is no ambiguity.

The complete program of the interface is listed below.

Step 1

First, we create a Java interface InterfaceExample in which we declare the method display():

public interface InterfaceExample {  
    public void display();  
}  

Step 2

Second, we create a Java class PublicExample in which we declare the method display():

public class InterfaceAccess implements InterfaceExample{  
    @Override  
    public void display() {  
        System.out.println("Interface class access the interface named  InterfaceExample");  
    }  
  
    public static void main(String args[]) {  
        InterfaceAccess i = new InterfaceAccess();  
        i.display();  
    }  
}  

The above program generates the following output.

Differentiation between abstract class and interface in Java

The similarity between Abstract class and Interface

  • Both abstract class and an interface are used for abstraction in Java.
  • We can’t instantiate both.

The complete program of abstract class and interface.

Step 1

Creating an interface that has 4 methods.

public interface Shape {  
    void draw();//bydefault, public and abstract  
    void print();  
    void show();  
    void display();  
}  

Step 2

Creating an abstract class that provides the implementation of one method of shape interface.

abstract class Circle implements Shape {  
    public void print() {  
        System.out.println("Print image");  
    }  
}  

Step 3

Create a subclass of an abstract class. Now, we need to provide the implementation of the rest of the methods

class Rectangle extends Circle {  
    public void draw() {  
        System.out.println("Draw image");  
    }  
    public void show() {  
        System.out.println("Show image");  
    }  
    public void display() {  
        System.out.println("Display image");  
    }  
} 

Step 4

Creating an Exampletest class that calls the methods of shape interface.

public class ExampleTest {  
public static void main(String args[]) {  
        Shape a = new Rectangle();  
        a.draw();  
        a.print();  
        a.show();  
        a.display();  
    }  
} 

The above program generates the following output.

Summary

In this article, we learned about abstract class and interface in Java programming language and how these are different from each other.

Thank you for reading!

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Learn Java Programming | Java Complete Tutorial for Beginners | Simpliv

Learn Java Programming | Java Complete Tutorial for Beginners | Simpliv

This program on Java programing from Simpliv’s experts uses Java and an Integrated Development Environment (IDE). Just download this, with their help of course, and open your way to hundreds of lines of source code, and hundreds of lines of comments. That is some Java programming learning!

Description
Taught by a Stanford-educated, ex-Googler, husband-wife team
This course will use Java and an Integrated Development Environment (IDE). Never fear, we have a detailed video on how to get this downloaded and set up.
Hundreds of lines of source code, and hundreds of lines of comments - just download and open in your IDE!
A Java course for everyone - accessible yet serious, to take you from absolute beginner to an early intermediate level

Let’s parse that.

This is a Java course for everyone. Whether you are a complete beginner (a liberal arts major, an accountant, doctor, lawyer) or an engineer with some programming experience but looking to learn Java - this course is right for you.
The course is accessible because it assumes absolutely no programming knowledge, and quickly builds up using first principles alone
Even so, this is a serious Java programming class - the gradient is quite steep, and you will go from absolute beginner to an early intermediate level
The course is also quirky. The examples are irreverent. Lots of little touches: repetition, zooming out so we remember the big picture, active learning with plenty of quizzes. There’s also a peppy soundtrack, and art - all shown by studies to improve cognition and recall.
What's Covered:

Programming Basics: What programming is, and a carefully thought-through tour of the basics of any programming. Installing and setting up an IDE and writing your first program
The Object-Oriented Paradigm: Classes, Objects, Interfaces, Inheritance; how an OO mindset differs from a functional or imperative programming mindset; the mechanics of OO - access modifiers, dynamic dispatch, abstract base classes v interfaces. The underlying principles of OO: encapsulation, abstraction, polymorphism
Threading and Concurrency: A deep and thorough study of both old and new ways of doing threading in Java: Runnables, Callables, Threads, processes, Futures, Executors.
Reflection, Annotations: The how, what and why - also the good and bad
Lambda Functions: Functional constructs that have made the crossover into the mainstream of Java - lambda functions, aggregate operators.
Modern Java constructs: Interface default methods; properties and bindings too. Also detailed coverage of Futures and Callables, as well as of Lambda functions, aggregation operators. JavaFX as contrasted with Swing.
Packages and Jars: The plumbing is important to understand too.
Language Features: Serialisation; why the Cloneable interface sucks; exception handling; the immutability of Strings; the Object base class; primitive and object reference types; pass-by-value and pass-by-object-reference.
Design: The MVC Paradigm, Observer and Command Design Patterns.
Swing: Framework basics; JFrames, JPanels and JComponents; Menus and menu handling; Trees and their nuances; File choosers, buttons, browser controls. A very brief introduction to JavaFX.
Programming Drills (code-alongs, with source code included)

Serious stuff:
A daily stock quote summariser: scrapes the internet, does some calculations, and outputs a nice, formatted Excel spreadsheet.
A News Curation app to summarise newspaper articles into a concise email snippet using serious Swing programming
Simple stuff:
Support with choosing a programming environment; downloading and setting up IntelliJ.
Simple hello-world style programs in functional, imperative and object-oriented paradigms.
Maps, lists, arrays. Creating, instantiating and using objects, interfaces
Who is the target audience?

Yep! Folks with zero programming experience - liberal arts majors, doctors, accountants, lawyers
Yep! Engineering students from non-CS majors looking to learn fairly serious programming
Nope! Experienced Java programmers - this class will be boring for you:)
Yep! Computer Science students or software engineers with no experience in Java, but experience in Python, C++ or even C#. You might need to skip over some bits, but in general the class will still have new learning to offer you :-)
Basic knowledge
No prior programming experience needed:)
The class will make use of Java and an IDE - never fear, we have a detailed video to walk you through the process of setting this up
What will you learn
Write Java programs of moderate complexity and sophistication (at an early to middling intermediate level)
Understand Object-Oriented programming concepts at the level where you can have intelligent design conversations with an experienced software engineer
Manage concurrency and threading issues in a multi-threaded environment
Create and modify files (including Excel spreadsheets) and download content from the internet using Java
Use Reflection, Annotations, Lambda functions and other modern Java language features
Build serious UI applications in Swing
Understand the Model-View-Controller paradigm, the Observer and Command Design patterns that are at the heart of modern UI programming
Gain a superficial understanding of JavaFX and Properties and Bindings
Understand the nuances of Java specific constructs in serialisation, exception-handling, cloning, the immutability of strings, primitive and object reference types
To continue:

Python vs Java : The programming language fight for dominance

Both languages offer something unique to the table. While they have their unique technical differences, they're essentially fully developed languages that have thriving communities aiding in innovation. Both languages are written differently as well, as they're utilized uniquely as per their running efficiencies. While one is extensively used for&nbsp;<a href="https://www.cuelogic.com/blog/data-and-machine-learning" target="_blank">Machine Learning</a>&nbsp;and Data analytics utilities, the other can be used to design great applications. Java is also an industry standard that can be used across various tech frameworks. It has an extensive code library that many developers can leverage to work off existing projects....

Anatomy of Python and Java

Both languages offer something unique to the table. While they have their unique technical differences, they're essentially fully developed languages that have thriving communities aiding in innovation. Both languages are written differently as well, as they're utilized uniquely as per their running efficiencies. While one is extensively used for Machine Learning and Data analytics utilities, the other can be used to design great applications. Java is also an industry standard that can be used across various tech frameworks. It has an extensive code library that many developers can leverage to work off existing projects....

( Read More )