0% found this document useful (0 votes)
701 views5 pages

Java Abstraction: Classes & Interfaces

1) Abstraction is achieved through abstract classes and interfaces. Abstract classes can contain abstract and non-abstract methods while interfaces only contain abstract methods. 2) Abstract methods have a signature but no body, and any class extending an abstract class must implement the abstract methods. 3) Interfaces provide a blueprint of a class with static constants and abstract methods, and are used to achieve abstraction and multiple inheritance. A class implements interfaces while interfaces can extend other interfaces.

Uploaded by

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

Java Abstraction: Classes & Interfaces

1) Abstraction is achieved through abstract classes and interfaces. Abstract classes can contain abstract and non-abstract methods while interfaces only contain abstract methods. 2) Abstract methods have a signature but no body, and any class extending an abstract class must implement the abstract methods. 3) Interfaces provide a blueprint of a class with static constants and abstract methods, and are used to achieve abstraction and multiple inheritance. A class implements interfaces while interfaces can extend other interfaces.

Uploaded by

sayyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Abstract Methods and Classes
  • Interface in Java
  • The Relationship Between Classes and Interfaces
  • Extending and Implementing Interfaces
  • Example to Demonstrate Implementing Interfaces

Object Oriented Programming and Design with Java 20CS43P

Week – 10
ABSTRACTION
ABSTRACT METHODS AND CLASSES
Abstraction is a process of hiding the implementation details from the user, only the functionality
will be visible to the user.
Here are two ways to achieve abstraction in java
1. Abstract class (partial abstraction)
2. Interface (full abstraction)
Abstract class in Java
A class that is declared as abstract is known as abstract class.
• Abstract classes may or may not contain abstract methods, i.e., methods without body
example like, public void get();
• But, if a class contain at least one abstract method, then the class must be declared abstract.
• If a class is declared abstract, it cannot be instantiated.
• To utilize an abstract class, you have to inherit it from another class, provide
implementations for the abstract methods in it.
• It needs to be extended and its method implemented.
• If you inherit an abstract class, you have to provide implementations for all the abstract
methods in it.
Syntax:

abstract class class_name


{
}

Abstract method
Method that are declared without any body within an abstract class are called abstract method.
The method body will be defined by its subclass. Abstract method can never be final and static.
Any class that extends an abstract class must implement all the abstract methods declared by the
super class.
• The abstract keyword is used to declare the method as abstract.
• You have to mention the abstract keyword before the method name in the method
declaration.
• An abstract method has a method signature, but no method body.
• Instead of curly braces, an abstract method will have a semicolon (;) at the end.

COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER


Object Oriented Programming and Design with Java 20CS43P

Syntax:

abstract return_type function_name (); // No definition

abstract class A
{
abstract void callme();
}
class B extends A
{
void callme()
{
[Link]("this is call me inside child.");
}
public static void main(String[] args)
{
B b = new B();
[Link](); // this is call me inside child
}
}

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.

Why use Java interface?

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

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

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.

COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER


Object Oriented Programming and Design with Java 20CS43P

Syntax:
interface <interface_name>
{

// declare constant fields


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

The Java compiler adds public and abstract keywords before the interface method. Moreover,
it adds public, static and final keywords before data members.

In other words, Interface fields are public, static and final by default, and the methods are public
and abstract.

The relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known


as multiple inheritance.

COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER


Object Oriented Programming and Design with Java 20CS43P

multiple inheritance is not supported in the case of class because of ambiguity. However, it is
supported in case of an interface because there is no ambiguity. It is because its implementation is
provided by the implementation class.
EXTENDING AND IMPLEMENTING INTERFACES
Like classes, interfaces can also be extended. i.e interfaces can be sub interfaced from other
interfaces.
In the below syntax name1 and name2 are interfaces so we use extends keyword.
interface name2 extends name1
{
Body of name2
}
Interfaces are used as “superclasses” whose properties are inherited by classes. It is therefore
necessary to create a class that inherits the given interfaces.
In the below syntax class cname implements the interface name1.
class cname implements name1
{
Body implementing interface name1
}
Or
In the below syntax class cname implements many interfaces ( name1,name2 and so on...)

class cname implements name1, name2,name3 ….


{
Body of classname
}

COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER


Object Oriented Programming and Design with Java 20CS43P

Example to demonstrate
• use of implementing interfaces
• use of extending interfaces.
interface Area
{
final static float pi=3.14f;
double compute(double x, double y);
}

interface Display extends Area


{
void display_result(double result);
}

class Rectangle implements Display


{
public double compute(double x, double y)
{
return(pi*x*y);
}

public void display_result(double result)


{
[Link]("The area is:"+result);
}
}

class InterfaceDemo
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
double res=[Link](10.2,20.4);
r1.display_result(res);
}
}

COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER

Common questions

Powered by AI

Abstract classes provide partial abstraction as they can have concrete methods (methods with a body) as well as abstract methods (methods without a body). On the other hand, interfaces provide full abstraction as they can only have abstract methods (with no method body). Furthermore, abstract classes cannot be instantiated and need to be extended, whereas interfaces need to be implemented by a class which then provides the implementation for all the declared methods .

Abstraction in Java supports effective software engineering by allowing developers to focus on high-level functionality without needing to manage implementation specifics. Abstraction simplifies complex systems by reducing what is visible to a more manageable level of detail, hence leading to more readable, maintainable, and modular code. By using abstract classes and interfaces, developers can define common structures and behaviors while allowing diverse implementations, fostering code reuse, ease of maintenance, and the ability to extend code bases with minimal disruption .

Interface fields are implicitly public, static, and final, meaning that they cannot be changed once set. Unlike class fields, they cannot represent states or hold values that change over time; they are essentially constants or configuration values. This restriction simplifies interfaces, focusing them only on defining capabilities through abstract methods without state management, which aligns with their role of grouping methods. This leads to a design where interfaces define behavior contracts, while state-keeping responsibilities are managed within concrete classes .

Interfaces in Java can be extended by other interfaces using the "extends" keyword, allowing a new interface to inherit and expand upon the method signatures of existing interfaces. This can lead to more modular and flexible design by enabling the creation of a hierarchy of related interfaces that provide more specific behavior contracts to be implemented by classes. For instance, an interface `Vehicle` might be extended by `LandVehicle` and `AirVehicle`, each defining specific methods like wheels and wings, focusing on the differential functionalities while sharing common vehicle behavior defined in `Vehicle` .

An abstract method is useful in an abstract class when different subclasses are expected to provide specific implementations for that method, according to their own functionality. For instance, if a superclass defines an abstract method `move()`, each subclass like `Car` and `Bicycle` can implement it differently to reflect their respective ways to move. An abstract method cannot be final because final methods cannot be overridden by subclasses, which would defeat the purpose of abstract methods needing subclass-specific implementations .

Multiple inheritance in Java is achieved through interfaces. A class can implement multiple interfaces, and an interface can extend multiple other interfaces. Since interfaces only define method signatures and not implementations, there is no ambiguity in multiple inheritance through interfaces. The ambiguity that would arise in class-based multiple inheritance does not occur because the actual implementation is provided by the implementation class, which resolves any potential conflicts .

All fields in an interface are required to be public, static, and final because interfaces are designed to define constants rather than variable states. By enforcing this constraint, interfaces focus solely on defining a contract that other classes can implement and ensure uniformity and safety across the implementation. This means that they can serve as shared configuration values or global constants, enhancing code reusability and maintainability because these constant values will remain unchanged regardless of implementing class behaviors .

Interfaces enable multiple inheritance in Java by allowing a class to implement more than one interface, thus inheriting the abstract method contracts of each. This is particularly useful when a class needs to have diverse functionalities modeled by different interface contracts. For example, consider a class `SmartDevice` that should implement both the `Camera` and `WiFi` interfaces to provide functionalities of taking pictures and connecting to Wi-Fi networks. By implementing these interfaces, `SmartDevice` can inherit the behaviors defined by both interfaces without ambiguity .

Interfaces support loose coupling by allowing a class to communicate with another class through a defined contract. By being restricted to communicating through the interface, classes are less dependent on each other's implementations. This means that a change in one class does not mandate changes in classes that interact with it via the interface, as long as the contract (interface) remains the same. As such, changes in implementation details are encapsulated and do not affect other parts of the program, promoting a more flexible and modular design .

The "abstract" keyword indicates that a method does not have an implementation (method body) and must be overridden in a subclass. When a method is declared as abstract using this keyword, any class that inherits this abstract class must provide concrete implementations for all abstract methods. This enforces a contract for subclasses to define specific behaviors for methods that are deemed abstract in the superclass .

You might also like