Chapter8 Abstraction

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Abstract Classes and Interfaces

ABSTRACT CLASSES
In a class hierarchy, the root class will always represent generic concepts that are
common to all subclasses. Mostly, we create objects from child classes and do not need
to create objects from the root class. As a developer, we know what objects are to be
created from those classes. However, other developers do not know that objects should
not be created from this root class

Defining Abstract Classes


Fortunately, Java provides a way to prevent object creation from generic classes, and
these generic classes are known as abstract classes. Any attempt to create an object
from an abstract class will cause a compiler error. The syntax for abstract class is
shown

[public][abstract] class class-name


{
// variables and methods to be accessible to subclasses
// can declare static variables and static methods
// abstract method declarations
}

Defining Abstract Methods


An abstract class can include abstract methods. Abstract classes cannot be
instantiated, but they can be subclassed. An abstract method is a method with only
signature (i.e., the method name, the list of arguments and the return type) without
implementation (i.e., the method’s body) and followed by a semicolon. You use the
keyword abstract to declare an abstract method.
If there is an abstract method in a class, then the class must be declared abstract.
When an abstract class is subclassed, the subclass usually provides implementations
for all of the abstract methods in its parent class. However, if it does not, the subclass
must also be declared abstract. The syntax for abstract method is depicted
[public][abstract] return-type method-name(arguments);

//FlowerObject. java
abstract class Flower
{
public String colour;
public void getColour()
{
System.out.println(colour);
}
public abstract void smell(); // abstract method
}
class Rose extends Flower
{
public void smell()
{
System.out.println(“Wow, this ” + colour + “ rose spreads romantic
fragrance...”);
}
}
class Jasmine extends Flower
{
public void smell()
{
System.out.println(“Wow, this ” + colour + “ jasmine spreads something
different fragrance...”);
}
}
public class FlowerObject
{
public static void main(String[] args)
{
Rose rose = new Rose();
rose.colour = “Red”;
rose.smell();

Jasmine jasmine = new Jasmine();


jasmine.colour = “White”;
jasmine.smell();
}
}

Output
Wow, this Red rose spreads romantic fragrance...
Wow, this White jasmine spreads something different fragrance...

INTERFACES
Interface is called pure abstract class. The reason is that interfaces can contain only
data members and abstract methods. In contrast, abstract classes can contain abstract
and nonabstract methods. Whether you declare or not, all data members in an interface
are public final. That means, they will be identified as constants and you can even
assign values. Similarly, all method declarations are public abstract by default. Interface
can also extend another interface.

1. Interfaces contain methods with no bodies.


2. You cannot create objects of an interface, but can declare variables of interface
type
3. An interface cannot be instantiated. You have to create a "subclass" that
implements an interface, and provide the actual implementation of all
the abstract methods. If the subclasses don’t provide implementation, then they
also have to be declared as abstract.
4. For interface, we use the keyword "implements" to derive a subclass.
5. An interface is a contract for what the classes can do. It, however, does not specify
how the classes should do it.
6. An interface provides a form, a protocol, a standard, a contract, a specification, a
set of rules, an interface, for all objects that implement it. It is
a specification and rules that any object implementing it agrees to follow.
7. When there is both an extends clause and implements clause, the extends clause
is always first.
8. Java supports only single inheritance. That is, a subclass can be derived from
one and only one superclass. Java does not support multiple inheritance to avoid
inheriting conflicting properties from multiple superclasses. However a class can
inherit from one superclass and inherit as many interfaces as it needs. Interfaces
are used as an alternative for multiple inheritance.

Declaring Interfaces
[ public ] [ abstract ] interface interface-name
{
// constants and method declarations
}

As an example, let us define an interface Wearable so that we can say flowers such as
rose and jasmine are flowers as well as wearable
interface Wearable
{
public void canWear();
}
Interface Naming Convention: Use an adjective (typically ends with "able")
consisting of one or more words. Each word shall be initial capitalized (camel-case). For
example, Serializable, Extenalizable, Movable, Clonable, Runnable, etc.

Implementing Interfaces
class class-name [extends superclass] [implements interfacename1, interface-
name2,…]
Now let us say flowers such as rose and jasmine are flowers as well as wearables. So
we create class Rose and Jasmine by extending class Flower and implementing
wearable

// WearableFlower.java
import java.util.*;
interface Wearable
{
public void canWear();
}
class Flower
{
public String colour;
public void getColour()
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter colour: ” );
colour = sc.nextLine();
}
}
class Rose extends Flower implements Wearable
{
public void smell()
{
System.out.println(“Wow, this ” + colour + “ rose spreads romantic
fragrance...”);
}
public void canWear()
{
System.out.println(“Girls can wear this ” + colour + “ rose ”);
}
}
public class WearableFlower
{
public static void main(String[] args)
{
Rose rose = new Rose();
rose.getColour();
rose.smell();
rose.canWear();
}
}

Output
Enter colour: red
Wow, this red rose spreads romantic fragrance...
Girls can wear this red rose
Problem: Flyable interface

We will now define the interface Flyable by specifying a constant wings and interface
method fly(). Further we will define classes such as Aircraft and Crow so that they can
fly.

//FlyableObject. java
interface Flyable
{
public int wings = 2; // by default, public final
public void fly(); // public abstract, by default
}
class Aircraft implements Flyable
{
public void fly()
{
System.out.println(“Aircrafts can fly 40000 feets with ” + wings + “ wings”);
}
}
class Bird
{
int leg; // number of legs
}
class Crow extends Bird implements Flyable
{
public void fly()
{
System.out.println(“Crows can fly 1000 feets with ” + wings + “ wings” + “
and without using its ” + legs + “ legs”);
}
}
public class FlyableObject
{
public static void main(String[] args)
{
Aircraft a = new Aircraft();
a.fly();

Crow c = new Crow();


c.legs = 2;
c.fly();
}
}

Output
Aircrafts can fly 40000 feets with 2 wings
Crows can fly 1000 feets with 2 wings and without using its 2 legs

Implementing Multiple Interfaces:

public class Circle extends Shape implements Movable, Adjustable {


// extends one superclass but implements multiple interfaces
.......
}

You might also like