KEMBAR78
Defining An Interface | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
9 views6 pages

Defining An Interface

The document explains abstract classes and methods in Java, highlighting that abstract classes cannot be instantiated and must contain abstract methods if any are defined. It also discusses interfaces, which are fully abstract and require implementation by classes, allowing for multiple inheritance. Key points include the use of the 'abstract' keyword, the necessity of overriding abstract methods in subclasses, and the advantages of using interfaces for abstraction and security.

Uploaded by

akileshwari R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Defining An Interface

The document explains abstract classes and methods in Java, highlighting that abstract classes cannot be instantiated and must contain abstract methods if any are defined. It also discusses interfaces, which are fully abstract and require implementation by classes, allowing for multiple inheritance. Key points include the use of the 'abstract' keyword, the necessity of overriding abstract methods in subclasses, and the advantages of using interfaces for abstraction and security.

Uploaded by

akileshwari R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstract classes and methods:

Java Abstract Class


 The abstract class in Java cannot be instantiated (we cannot create objects of abstract
classes). We use the abstract keyword to declare an abstract class. For example,

// create an abstract class


abstract class Language {
// fields and methods}

An abstract class can have both the regular methods and abstract methods. For example,
abstract class Language {

// abstract method
abstract void method1();

// regular method
void method2() {
System.out.println("This is regular method");
}}
To know about the non-abstract methods, visit Java methods. Here, we will learn about abstract
methods.

Java Abstract Method


 A method that doesn't have its body is known as an abstract method. We use the
same abstract keyword to create abstract methods. For example,
 abstract void display();
 Here, display() is an abstract method. The body of display() is replaced by ;.
 If a class contains an abstract method, then the class should be declared abstract. Otherwise,
it will generate an error. For example,
class Language {
// abstract method
abstract void method1(); }

Example: Java Abstract Class and Method


Though abstract classes cannot be instantiated, we can create subclasses from it. We can then access
members of the abstract class using the object of the subclass. For example,
abstract class Language {

// method of abstract class


public void display() {
System.out.println("This is Java Programming"); }}

class Main extends Language {


public static void main(String[] args) {
// create an object of Main
Main obj = new Main();
// access method of abstract class
// using object of Main class
obj.display();
}}
Run Code
Output
This is Java programming
 In the above example, we have created an abstract class named Language. The class contains
a regular method display().
 We have created the Main class that inherits the abstract class. Notice the statement,
 obj.display();
 Here, obj is the object of the child class Main. We are calling the method of the abstract class
using the object obj.

Implementing Abstract Methods


If the abstract class includes any abstract method, then all the child classes inherited from the abstract
superclass must provide the implementation of the abstract method. For example,
abstract class Animal {
abstract void makeSound();

public void eat() {


System.out.println("I can eat.");
}}

class Dog extends Animal {

// provide implementation of abstract method


public void makeSound() {
System.out.println("Bark bark");
}}

class Main {
public static void main(String[] args) {

// create an object of Dog class


Dog d1 = new Dog();
d1.makeSound();
d1.eat();
}}
Run Code
Output
Bark bark
I can eat.
 In the above example, we have created an abstract class Animal. The class contains an
abstract method makeSound() and a non-abstract method eat().
 We have inherited a subclass Dog from the superclass Animal. Here, the
subclass Dog provides the implementation for the abstract method makeSound().
 We then used the object d1 of the Dog class to call methods makeSound() and eat().
 Note: If the Dog class doesn't provide the implementation of the abstract
method makeSound(), Dog should also be declared as abstract. This is because the
subclass Dog inherits makeSound() from Animal.

Java Abstraction
 The major use of abstract classes and methods is to achieve abstraction in Java.
 Abstraction is an important concept of object-oriented programming that allows us to hide
unnecessary details and only show the needed information.
 This allows us to manage complexity by omitting or hiding details with a simpler, higher-
level idea.
 A practical example of abstraction can be motorbike brakes. We know what brake does.
When we apply the brake, the motorbike will stop. However, the working of the brake is kept
hidden from us.
 The major advantage of hiding the working of the brake is that now the manufacturer can
implement brake differently for different motorbikes, however, what brake does will be the
same.
Let's take an example that helps us to better understand Java abstraction.
Example 3: Java Abstraction
abstract class MotorBike {
abstract void brake();}

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("SportsBike Brake");}}

class MountainBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("MountainBike Brake"); }}

class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
s1.brake(); }}
Run Code
Output:
MountainBike Brake
SportsBike Brake
 In the above example, we have created an abstract super class MotorBike. The
superclass MotorBike has an abstract method brake().
 The brake() method cannot be implemented inside MotorBike. It is because every bike has
different implementation of brakes. So, all the subclasses of MotorBike would have different
implementation of brake().
 So, the implementation of brake() in MotorBike is kept hidden.
 Here, MountainBike makes its own implementation of brake() and SportsBike makes its own
implementation of brake().
Key Points to Remember
 We use the abstract keyword to create abstract classes and methods.
 An abstract method doesn't have any implementation (method body).
 A class containing abstract methods should also be abstract.
 We cannot create objects of an abstract class.
 To implement features of an abstract class, we inherit subclasses from it and create objects of
the subclass.
 A subclass must override all abstract methods of an abstract class. However, if the subclass is
declared abstract, it's not mandatory to override abstract methods.
 We can access the static attributes and methods of an abstract class using the reference of the
abstract class. For example, Animal.staticMethod();

Defining an interface

 An interface is a completely "abstract class" that is used to group related methods with empty
bodies:
Example
// interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)}

To access the interface methods, the interface must be "implemented" (kinda like inherited) by
another class with the implements keyword (instead of extends).

The body of the interface method is provided by the "implement" class:


Example
// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)}

// Pig "implements" the Animal interface


class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee"); }

public void sleep() {


// The body of sleep() is provided here
System.out.println("Zzz");
}}

class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep() }}

Implement interfaces

Notes on Interfaces:
 Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not
possible to create an "Animal" object in the MyMainClass)
 Interface methods do not have a body - the body is provided by the "implement" class
 On implementation of an interface, you must override all of its methods
 Interface methods are by default abstract and public
 Interface attributes are by default public, static and final
 An interface cannot contain a constructor (as it cannot be used to create objects)
Why And When To Use Interfaces?
 To achieve security - hide certain details and only show the important details of an object (interface).
 Java does not support "multiple inheritance" (a class can only inherit from one superclass). However,
it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To
implement multiple interfaces, separate them with a comma (see example below).

Multiple Interfaces
To implement multiple interfaces, separate them with a comma:
Example
interface FirstInterface {
public void myMethod(); // interface method}

interface SecondInterface {
public void myOtherMethod(); // interface method}

class DemoClass implements FirstInterface, SecondInterface {


public void myMethod() {
System.out.println("Some text.."); }
public void myOtherMethod() {
System.out.println("Some other text..."); }}

class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod(); }}
Implementing Multiple Interfaces
In Java, a class can also implement multiple interfaces. For example,
interface A {
// members of A}
interface B {
// members of B}
class C implements A, B {
// abstract members of A
// abstract members of B}

Extending an Interface
Similar to classes, interfaces can extend other interfaces. The extends keyword is used for extending
interfaces. For example,

interface Line {
// members of Line interface}
// extending interface
interface Polygon extends Line {
// members of Polygon interface
// members of Line interface}
Here, the Polygon interface extends the Line interface. Now, if any class implements Polygon, it should
provide implementations for all the abstract methods of both Line and Polygon.

Extending Multiple Interfaces


An interface can extend multiple interfaces. For example,
interface A {...}
interface B { ... }
interface C extends A, B { ...}

Advantages of Interface in Java


 Now that we know what interfaces are, let's learn about why interfaces are used in Java.
 Similar to abstract classes, interfaces help us to achieve abstraction in Java.
Here, we know getArea() calculates the area of polygons, but the way area is calculated is
different for different polygons. Hence, the implementation of getArea() is independent of one
another.
 Interfaces provide specifications that a class (which implements it) must follow.
In our previous example, we used getArea() as a specification inside the interface Polygon. This
is like setting a rule that we should be able to get the area of every polygon.
Now any class that implements the Polygon interface must provide an implementation for
the getArea() method.
 Interfaces are also used to achieve multiple inheritance in Java. For example,
interface Line {…}
interface Polygon {…}
class Rectangle implements Line, Polygon {…}

Here, the class Rectangle is implementing two different interfaces. This is how we achieve multiple
inheritance in Java.

You might also like