Chapter8 Abstraction
Chapter8 Abstraction
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
//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();
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.
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();
Output
Aircrafts can fly 40000 feets with 2 wings
Crows can fly 1000 feets with 2 wings and without using its 2 legs