Abstract class and Interface
Abstract class and Interface
Interfaces
In Java
Abstract Methods and Classes
• A class can define a method without an
implementation, forcing subclasses to
implement it.
• Such a method, and the class containing it,
are called abstract and must be tagged with
the abstract modifier.
• This is commonly done for very general
classes.
Abstract Methods and Classes
• Example:
public abstract class Person {
private String name;
public Person(String name) {
this.name = name; }
public final String getName() {
return name; }
public abstract int getId();
}
• Any class extending Person must either supply an
implementation of the getId method or be itself
declared as abstract.
• Abstract class can have nonabstract methods, such
as the getName method.
Abstract Class
• It is not possible to construct an instance of an
abstract class.
• For example, the call
variable.
● Person p = new Student("Fred", 1729);
Bank b;
b=new SBI();
Class c1 = b.getClass();
//Printing object using c1.
System.out.println(c1.getName());
System.out.println(“RoI is: "+b.getRateOfInterest()+" %");b=new
PNB();
Abstract Calendar Class and
Its GregorianCalendar subclass
Abstract Calendar Class and Its
GregorianCalendar subclass
• An instance of java.util.Date represents a
specific instant in time with millisecond precision.
• java.util.Calendar is an abstract base class
for extracting detailed information such as year,
month, date, hour, minute and second from a Date
object.
• Subclasses of Calendar can implement specific
calendar systems such as Gregorian calendar,
Lunar Calendar and Jewish calendar.
• Currently, java.util.GregorianCalendar for
the Gregorian calendar is supported in the Java API.
Calendar Class
• The java.util.Calendar class was added in
Java on JDK 1.4 in an attempt to fix some flaws of
the java.util.Date class.
• An arbitrary date may be created using new
GregorianCalendar(2016, Calendar.JUNE,
11) constructor, as opposed to Date class where
the year starts from 1900 and Month was starting
from zero.
Calendar Class
• The Calendar class provides support for:
• Maintaining a set of calendar fields such as
YEAR, MONTH, DAY_OF_MONTH, HOUR,
MINUTE, SECOND, MILLISECOND; and
manipulating these calendar fields, such as
getting the date of the previous week, roll
forward by 3 days.
Calendar Class
• Calendar provides internationalization
support.
• Calendar.getInstance(): return a
Calendar instance based on the current time
in the default time zone with the default
locale.
Calendar.getInstance(TimeZone zone)
Calendar.getInstance(Locale aLocale)
Calendar.getInstance(TimeZone zone,
Locale aLocale)
The get Method in Calendar Class
• The get(int field) method defined in the Calendar
class is useful to extract the date and time information from
a Calendar object.
• The fields are defined as constants, as shown in the
following.
Example
import java.util.Calendar;
public class GetYMDHMS {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// You cannot use Date class to extract individual Date
fields
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); // 0 to 11
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
System.out.printf("Now is %4d/%02d/%02d
%02d:%02d:%02d\n",
year, day, month+1, hour, minute, second);
}
}
Calendar Class
• The java.util.Calendar class is an
abstract class and you obtain the instance of
a Calendar local to your timezone and Locale
by calling the static getInstance() method
of java.util.Calendar class.
• This method generally returns an instance of
GregorianCalendar class.
GregorianCalendar
• The Gregorian calendar is the calendar which
is used today. It came into effect on October
15, 1582, in some countries.
– In Julian calendar, every four years is a leap year.
• In the Gregorian calendar, a leap year is a
year that is divisible by 4 but not divisible by
100, or it is divisible by 400, i.e., the Gregorian
calendar omits century years which are not
divisible by 400 (removing 3 leap years (or 3
days) for every 400 years).
The GregorianCalendar Class
• You can use new GregorianCalendar()
to construct a default GregorianCalendar
with the current time and use new
GregorianCalendar(year, month,
date) to construct a GregorianCalendar
with the specified year, month, and date.
• The month parameter is 0-based, i.e., 0 is for
January.
Interfaces
• What is an interface?
• Why is an interface useful?
• How do you define an interface?
• How do you use an interface?
What is an interface?
Why is an interface useful?
• An interface is a classlike construct that
contains only constants and abstract
methods.
• An interface is similar to an abstract class,
but the intent of an interface is to specify
common behavior for objects.
• A Class Can Extend Only One Other Class
But Can Implement Many Interfaces
Define an Interface
• To distinguish an interface from a class, Java uses
the following syntax to define an interface:
public interface InterfaceName {
constant declarations;
abstract method signatures;
}
public class Circle implements Shape
{ private double radius;
public Circle(double r){
this.radius = r;}
public
interface Drawable {
int RED = 1;
int GREEN = 2;
int BLUE = 3;
int BLACK = 4;
int WHITE = 5;
void draw(int color); }
class Draw
{
public static void main(String[] args)
{ Drawable[] drawables
= new Drawable[] { new Circle(10, 20, 15),
new Circle(30, 20, 10),
new Rectangle(5, 8, 8, 9)
};
for (int i = 0; i < drawables.length;
i++)
Another Example of Interface
interface printable{
void print();
}
-------------------------------------------------
abstract class Animal {
/** Return animal sound */
public abstract String sound();
}
Array of Objects
Object[] objects={new Tiger(), new Barbet(), new
Apple()};
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof Edible)
System.out.println(((Edible)objects[i]).howToEat());
// constructor
public Invoice(String partNumber, String partDescription, int
quantity,double pricePerItem)
{
if (quantity < 0) // validate quantity
throw new IllegalArgumentException("Quantity must be >= 0");
this.quantity = quantity;
this.partNumber = partNumber;
public String getPartNumber()
{ return partNumber; // should validate
}
// get description
public String getPartDescription()
{ return partDescription;
}
// set quantity
public void setQuantity(int quantity)
{
if (quantity < 0) // validate
throw new IllegalArgumentException("Quantity must be >= 0");
this.quantity = quantity;
}
// get quantity
public int getQuantity()
{ return quantity;
}
// set price per item
Omitting Modifiers in Interfaces
• All data fields are public final static and all methods are
public abstract in an interface.
• For this reason, these modifiers can be omitted, as
shown below: