DEV Community

Neelakandan R
Neelakandan R

Posted on

Keyword: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();  
    }   
Enter fullscreen mode Exit fullscreen mode

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();

    }



}

Enter fullscreen mode Exit fullscreen mode
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

    }


}
Enter fullscreen mode Exit fullscreen mode

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(); 

}
Enter fullscreen mode Exit fullscreen mode

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");
    }

}

Enter fullscreen mode Exit fullscreen mode

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.(TBD)

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:

 interface <interface_name>{  

        // declare constant fields  
        // declare methods that abstract   
        // by default.  
    }  

Enter fullscreen mode Exit fullscreen mode

Example:

 interface Animal {  
        void eat();  
        void sleep();  
    }  

Enter fullscreen mode Exit fullscreen mode

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.

class One {
    public void methodOne()
    {

        // Some Functionality
    }
}

class Two extends One {

    public static void main(String args[])
    {
        Two t = new Two();

        // Calls the method one
        // of the above class
        t.methodOne();
    }
}

Enter fullscreen mode Exit fullscreen mode

“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.

/

/ Defining an interface
interface One {
    public void methodOne();
}

// Defining the second interface
interface Two {
    public void methodTwo();
}

// Implementing the two interfaces
class Three implements One, Two {
    public void methodOne()
    {

        // Implementation of the method
    }

    public void methodTwo()
    {

        // Implementation of the method
    }
}

Enter fullscreen mode Exit fullscreen mode

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

Example:

package chennai.velachery;

public interface EmployeeRules {

    int salary = 25000; //static, final, public
    int leaves = 20;
    int hours = 8; 

     void work();

     void takeLeave();

     void getSalary(); 



}
Enter fullscreen mode Exit fullscreen mode

package chennai.velachery;

public class Employee1 implements EmployeeRules{
    int empId; 
    String address; 
    public static void main(String[] args) {
        Employee1 emp1 = new Employee1(); 
        emp1.getSalary();
        System.out.println(Employee1.salary); 
        //Employee1.salary = 45000; 
        emp1.hear_songs();

    }
    public void hear_songs()
    {
        System.out.println("listening songs");
    }

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

    }

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


    }

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


    }

}

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)