ABSTRACT CLASSES
AND
ABSTRACT
METHODS
ABSTRACTION
 Abstraction basically is the art of hiding implementation
details from user and provide the user what they want.
 Let’s try to understand with real world example.
 Most of us are quite fond of owning a car.
 When we go to place order for the car we are really not
interested to understand very fine details of implementation of
each and every component insider the car engine, Gear box
etc.,
 We leave those technical details and implementation for
manufacturing engineers and mechanics to understand we are
simply interested in the car so does the manufacturing
company.
 They are interested to exactly provide us what we want and
hide the fine implementation details from us.
WHAT IS AN ABSTRACT CLASS?
 Super classes are created through the process called
"generalization"
 Common features (methods or variables) are factored out of
object classifications (ie. classes).
 Those features are formalized in a class. This becomes the
superclass
 The classes from which the common features were taken
become subclasses to the newly created super class
 Often, the superclass does not have a "meaning" or
does not directly relate to a "thing" in the real world
 It is an artifact of the generalization process
 Because of this, abstract classes cannot be
instantiated
 They act as place holders for abstraction
 An abstract class is a class which contains the abstract
keyword in its declaration.
 Abstract classes may or may not contain abstract
methods, i.e., methods without body ( public void get(); )
 But, if a class has at least one abstract method, then the
class must be declared abstract.
 If a class is declared abstract, it cannot be instantiated.
 To use an abstract class, you have to inherit it from
another class, provide implementations to the abstract
methods in it.
 If you inherit an abstract class, you have to provide
implementations to all the abstract methods in it.
WHY ABSTRACT CLASSES USED?
 Abstract classes are used heavily in Design Patterns
 Creational Patterns: Abstract class provides interface for
creating objects. The subclasses do the actual object
creation
 Structural Patterns: How objects are structured is handled
by an abstract class. What the objects do is handled by the
subclasses
 Behavioural Patterns: Behavioural interface is declared in
an abstract superclass. Implementation of the interface is
provided by subclasses.
 Be careful not to over use abstract classes
 Every abstract class increases the complexity of your
design
 Every subclass increases the complexity of your
design
 Ensure that you receive acceptable return in terms of
functionality given the added complexity.
Vehicle
- make: String
- model: String
- tireCount: int
Car
- trunkCapacity:
int
Truck
- bedCapacity: int
Note: UML represents abstract
classes by displaying their name
in italics.
ABSTRACT CLASS
 In the following example, the subclasses represent
objects taken from the problem domain.
 The superclass represents an abstract concept that
does not exist "as is" in the real world.
 Abstract superclass:
public abstract class Vehicle
{
private String make;
private String model;
private int
tireCount; [...]
public class Car extends Vehicle
{
private int
trunkCapacity; [...]
Vehicle
- make: String
- model: String
- tireCount: int
Car
- trunkCapacity: int
Truck
- bedCapacity: int
public class Truck extends Vehicle
{
private int
bedCapacity; [...] Often referred to
as "concrete"
classes
DEFINING ABSTRACT CLASSES
EXAMPLE OF ABSTRACT CLASS
public abstract class Employee
{
private String name;
private String address;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
}
NOW LETS TRY TO INSTANTIATE THE
EMPLOYEE CLASS IN THE FOLLOWING WAY −
public class AbstractDemo
{
public static void main(String[] args)
{
Employee e = new Employee(“John K.“);
System.out.println("n Call Employee ");
e.mailCheck();
}
}
ABSTRACT METHODS
 Methods can also be abstracted
 An abstract method is one to which a signature has been
provided, but no implementation for that method is
given.
 An Abstract method is a placeholder. It means that we
declare that a method must exist, but there is no
meaningful implementation for that methods within this
class
 Any class which contains an abstract method MUST
also be abstract
 Any class which has an incomplete method definition
cannot be instantiated (ie. it is abstract).
 Abstract classes can contain both concrete and abstract
methods.
 If a method can be implemented within an abstract class,
and implementation should be provided.
 abstract keyword is used to declare the method as
abstract.
 You have to place the abstract keyword before the
method name in the method declaration.
 An abstract method contains a method signature, but
no method body.
 Instead of curly braces, an abstract method will have
a semoi colon (;) at the end.
RetailSale
- computeValue(): int
StockTrade
- computeValue(): int
 In the following example, a Transaction's value can be
computed, but there is no meaningful implementation
that can be defined within the Transaction class.
Transaction
- computeValue(): int
public abstract class Transaction
{
public abstract int computeValue();
public class RetailSale extends Transaction
{
public int computeValue()
{
[...]
Transaction
- computeValue(): int
RetailSale
-
computeValue
(): int
StockTrade
- computeValue(): int
public class StockTrade extends Transaction
{
public int computeValue()
{
[...]
Note: no implementation
DEFINING ABSTRACT METHODS
EXAMPLE
 Following is an example of the abstract method.
 Declaring a method as abstract has two consequences−
 The class containing it must be declared as abstract.
 Any class inheriting the current class must either override
the abstract method or declare itself as abstract.
public abstract class Employee
{
private String name;
public abstract double computePay();
}
 Suppose Salary class inherits the Employee class, then
it should implement computePay()
public class Salary extends Employee
{
private double salary; double
computePay()
{
System.out.println("Computing salary " + getName());
return salary/52;
}
}
THANK YOU

Abstract Classes and Abstract Methods.pptx

  • 1.
  • 2.
    ABSTRACTION  Abstraction basicallyis the art of hiding implementation details from user and provide the user what they want.  Let’s try to understand with real world example.  Most of us are quite fond of owning a car.  When we go to place order for the car we are really not interested to understand very fine details of implementation of each and every component insider the car engine, Gear box etc.,  We leave those technical details and implementation for manufacturing engineers and mechanics to understand we are simply interested in the car so does the manufacturing company.  They are interested to exactly provide us what we want and hide the fine implementation details from us.
  • 3.
    WHAT IS ANABSTRACT CLASS?  Super classes are created through the process called "generalization"  Common features (methods or variables) are factored out of object classifications (ie. classes).  Those features are formalized in a class. This becomes the superclass  The classes from which the common features were taken become subclasses to the newly created super class  Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world  It is an artifact of the generalization process  Because of this, abstract classes cannot be instantiated  They act as place holders for abstraction
  • 4.
     An abstractclass is a class which contains the abstract keyword in its declaration.  Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )  But, if a class has at least one abstract method, then the class must be declared abstract.  If a class is declared abstract, it cannot be instantiated.  To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.  If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
  • 5.
    WHY ABSTRACT CLASSESUSED?  Abstract classes are used heavily in Design Patterns  Creational Patterns: Abstract class provides interface for creating objects. The subclasses do the actual object creation  Structural Patterns: How objects are structured is handled by an abstract class. What the objects do is handled by the subclasses  Behavioural Patterns: Behavioural interface is declared in an abstract superclass. Implementation of the interface is provided by subclasses.
  • 6.
     Be carefulnot to over use abstract classes  Every abstract class increases the complexity of your design  Every subclass increases the complexity of your design  Ensure that you receive acceptable return in terms of functionality given the added complexity.
  • 7.
    Vehicle - make: String -model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int Note: UML represents abstract classes by displaying their name in italics. ABSTRACT CLASS  In the following example, the subclasses represent objects taken from the problem domain.  The superclass represents an abstract concept that does not exist "as is" in the real world.  Abstract superclass:
  • 8.
    public abstract classVehicle { private String make; private String model; private int tireCount; [...] public class Car extends Vehicle { private int trunkCapacity; [...] Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int public class Truck extends Vehicle { private int bedCapacity; [...] Often referred to as "concrete" classes DEFINING ABSTRACT CLASSES
  • 9.
    EXAMPLE OF ABSTRACTCLASS public abstract class Employee { private String name; private String address; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; } public double computePay() { System.out.println("Inside Employee computePay"); return 0.0; } }
  • 10.
    NOW LETS TRYTO INSTANTIATE THE EMPLOYEE CLASS IN THE FOLLOWING WAY − public class AbstractDemo { public static void main(String[] args) { Employee e = new Employee(“John K.“); System.out.println("n Call Employee "); e.mailCheck(); } }
  • 11.
    ABSTRACT METHODS  Methodscan also be abstracted  An abstract method is one to which a signature has been provided, but no implementation for that method is given.  An Abstract method is a placeholder. It means that we declare that a method must exist, but there is no meaningful implementation for that methods within this class  Any class which contains an abstract method MUST also be abstract  Any class which has an incomplete method definition cannot be instantiated (ie. it is abstract).
  • 12.
     Abstract classescan contain both concrete and abstract methods.  If a method can be implemented within an abstract class, and implementation should be provided.  abstract keyword is used to declare the method as abstract.  You have to place the abstract keyword before the method name in the method declaration.  An abstract method contains a method signature, but no method body.  Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
  • 13.
    RetailSale - computeValue(): int StockTrade -computeValue(): int  In the following example, a Transaction's value can be computed, but there is no meaningful implementation that can be defined within the Transaction class. Transaction - computeValue(): int
  • 14.
    public abstract classTransaction { public abstract int computeValue(); public class RetailSale extends Transaction { public int computeValue() { [...] Transaction - computeValue(): int RetailSale - computeValue (): int StockTrade - computeValue(): int public class StockTrade extends Transaction { public int computeValue() { [...] Note: no implementation DEFINING ABSTRACT METHODS
  • 15.
    EXAMPLE  Following isan example of the abstract method.  Declaring a method as abstract has two consequences−  The class containing it must be declared as abstract.  Any class inheriting the current class must either override the abstract method or declare itself as abstract. public abstract class Employee { private String name; public abstract double computePay(); }
  • 16.
     Suppose Salaryclass inherits the Employee class, then it should implement computePay() public class Salary extends Employee { private double salary; double computePay() { System.out.println("Computing salary " + getName()); return salary/52; } }
  • 17.