search
HomeJavajavaTutorialKeyword:Abstract-Extends,Interface-Implements

Java abstract Keyword

The abstract keyword is used to achieve abstraction in Java. It is a non-access modifier which is used to create abstract class and method.

The role of an abstract class is to contain abstract methods. However, it may also contain non-abstract methods. The method which is declared with abstract keyword and doesn't have any implementation is known as an abstract method.

Syntax:-

    abstract class Employee  
    {  
    abstract void work();  
    }   

Note - We cannot declare abstract methods in non abstract class.

Rules of abstract keyword
Don'ts

An abstract keyword cannot be used with variables and constructors.
If a class is abstract, it cannot be instantiated.
If a method is abstract, it doesn't contain the body.
We cannot use the abstract keyword with the final.
We cannot declare abstract methods as private.
We cannot declare abstract methods as static.
An abstract method can't be synchronized.

Do's(TBD)

An abstract keyword can only be used with class and method.
An abstract class can contain constructors and static methods.
If a class extends the abstract class, it must also implement at least one of the abstract method.
An abstract class can contain the main method and the final method.
An abstract class can contain overloaded abstract methods.
We can declare the local inner class as abstract.
We can declare the abstract method with a throw clause.

What is abstract class:

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

What is abstract method:

An abstract method is a method that is declared without an implementation, (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);

*abstraction: *

Its main goal is to handle complexity by hiding unnecessary details from the user.

Showing Only necessary data and hiding unwanted details from the user.

For an abstract class, we have child class. Can this child class be also an abstract class?
Yes, in that case, Child class cannot be instantiated.

Reference:https://www.javatpoint.com/abstract-keyword-in-java

package chennai.velachery;

public abstract class Parent {

    abstract void getJob();

    void studyWell()
    {
        System.out.println("Focus On Maths, Science"); 
    }
    static void display()
    {
        System.out.println("Hi");
    }
    public static void main(String[] args) {
        Parent.display();
//      Parent pp = new Parent(); 
//      pp.display();
//      pp.studyWell(); 
//      pp.getJob();

    }



}

package chennai.velachery;

public class Child extends Parent {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Child ch = new Child(); 
        ch.getJob();
        ch.studyWell();
        Child.display();
    }

    @Override //Annotation : Metadata
    //Data about data
    void getJob() {
        // TODO Auto-generated method stub

    }


}

Example 2:

package chennai.velachery;

public abstract class Parent1 {

    public void watch_tv()
    {
        System.out.println("Watching TV");
    }
    public void chat_with_friends()
    {
        System.out.println("Chatting with neighbours");
    }
    public abstract void cook(); 

}

package chennai.velachery;

public class Child1 extends Parent1{

    public static void main(String[] args) {
//      Child1 ch = new Child1(); 
//      ch.cook();
//      ch.chat_with_friends(); 
//      ch.watch_tv();
//      ch.work();

        Child1 ch = new Child1(); 
        //Dynamic Binding 
        //Parent Class Reference points to Child class Memory
        Parent1 parent = new Child1();  

        parent.watch_tv();
        parent.chat_with_friends();
        parent.cook();
        //parent.work(); 
    }
    public void watch_tv()
    { //Runtime Polymorphism
        System.out.println("Watching OTT");
    }

    @Override
    public void cook() {
        // TODO Auto-generated method stub
        System.out.println("Cooking");

    }

    public void work()
    {
        System.out.println("Java Projects");
    }

}

Interface in Java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

Java Interface also represents the IS-A relationship.

It cannot be instantiated just like the abstract class.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

Image description

How to declare an interface?

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.

*INTERFACE: *

an interface is a group of related methods with empty bodies
100% Abstraction:

Syntax:

    abstract class Employee  
    {  
    abstract void work();  
    }   

Example:

package chennai.velachery;

public abstract class Parent {

    abstract void getJob();

    void studyWell()
    {
        System.out.println("Focus On Maths, Science"); 
    }
    static void display()
    {
        System.out.println("Hi");
    }
    public static void main(String[] args) {
        Parent.display();
//      Parent pp = new Parent(); 
//      pp.display();
//      pp.studyWell(); 
//      pp.getJob();

    }



}

Reference:https://www.javatpoint.com/interface-in-java

Extends vs Implements in Java:

In Java, the extends keyword is used to inherit all the properties and methods of the parent class while the implements keyword is used to implement the method defined in an interface.

**
Difference Between extends and implements Keyword**

Image description

“extends” Keyword in Java

In Java, the extends keyword is used to indicate that the class which is being defined is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the parent class to the subclass. In Java, multiple inheritances are not allowed due to ambiguity. Therefore, a class can extend only one class to avoid ambiguity.

package chennai.velachery;

public class Child extends Parent {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Child ch = new Child(); 
        ch.getJob();
        ch.studyWell();
        Child.display();
    }

    @Override //Annotation : Metadata
    //Data about data
    void getJob() {
        // TODO Auto-generated method stub

    }


}

“implements” Keyword in Java

In Java, the implements keyword is used to implement an interface. An interface is a special type of class which implements a complete abstraction and only contains abstract methods. To access the interface methods, the interface must be “implemented” by another class with the implements keyword and the methods need to be implemented in the class which is inheriting the properties of the interface. Since an interface is not having the implementation of the methods, a class can implement any number of interfaces at a time.

/

package chennai.velachery;

public abstract class Parent1 {

    public void watch_tv()
    {
        System.out.println("Watching TV");
    }
    public void chat_with_friends()
    {
        System.out.println("Chatting with neighbours");
    }
    public abstract void cook(); 

}

Reference:https://www.geeksforgeeks.org/extends-vs-implements-in-java/

Example:

    abstract class Employee  
    {  
    abstract void work();  
    }   

package chennai.velachery;

public abstract class Parent {

    abstract void getJob();

    void studyWell()
    {
        System.out.println("Focus On Maths, Science"); 
    }
    static void display()
    {
        System.out.println("Hi");
    }
    public static void main(String[] args) {
        Parent.display();
//      Parent pp = new Parent(); 
//      pp.display();
//      pp.studyWell(); 
//      pp.getJob();

    }



}

Abstract Class u>
0 to 100% Abstraction

abstract keyword

non-static variables

can be present

*
*vs Interface
100% Abstraction
No abstract keyword
all variables are static,
final and public

The above is the detailed content of Keyword:Abstract-Extends,Interface-Implements. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.