Você está na página 1de 5

Abstract Class in Java :

Create a super class that only defines a generalized form that will be shared by all of
its subclass leaving it to each subclass to fill in the details.

The key idea with an abstract class is useful when there is common functionality
that's like to implement in a super class and some behavior is unique to specific
classes.
So you implement the super class as an abstract class and define methods that have
common subclasses. Then you implement each subclass by extending the abstract
class and add the methods unique to the class.

Eg:

Courses

Lecture Lab Independent


Course Course Course

Start out with Courses super class, determine up front all the necessary and common
attributes needed by the class.

String courseName enrollStudent


String courseNumber assignInstructor
int creditValue establishCourseSchedule
CollectionType enrolledStudent
Professor instructor

Some behaviors may be generic enough so that we can afford to program them in
detail for the Courses class, knowing that any sub classes of the Courses class will be
able to inherit these methods as it is without needing to override them.

public void enrollStudent(Student s)


{
enrolled.add(s);
}
public void assignInstructor(Professor p)
{
setInstructor(p);
}

When you attempt to program a generic version of the establishCourseSchedule()


method, the business rules governing how to establish a course schedule differ
significantly for different types of courses

A lecture course may meet only once a week for 3 hours at a time
A lab course may meet twice a week for 2 hours each time
An independent course may meet on a custom schedule that has jointly negotiated
by a given student & professor

It would be a waste of time to bother trying to program a generic one-size-fit-all version


of the establishCourseSchedule() method within the Courses class

An abstract class is used to define what behaviors a class is required to perform


without having to provide an explicit implementation of how each and every behavior
will be carried out.
keyword : abstract
syntax:
To declare abstract class

public abstract class classname


{
abstract method;
non-abstract method
{
Statements;
}
variables;
}

syntax:
To declare abstract method

accessSpecifier abstract returntype method_name(argument_list);

1. No method body is present [bodiless (or) header-only methods]


2. There can be no objects of an abstract class
i.e cannot directly create an object using the new operator
it is possible to create a reference to an abstract class so that it can be used to point
to a subclass object.
3. Any subclass of an abstract class either implements all the abstract methods in the
super class or be itself declared abstract
Eg;
public abstract class Courses
{
private String courseName;
private String courseNumber;
private int creditValue;
private ArrayList enrolledStudents;
private Professor instructor

public void enrollStudent(Student s)


{
enrolledStudent.add(s);
}
public void assignInstructor(Professor p)
{
setInstructor(p);
}
public abstract void establishCourseSchedule(String startDate, String endDate);
}

By providing an abstract establishCoursesSchedule() method, we have specified a service


that all types of Courses objects must be able to perform but without pinning down the
private details of how the service should be performed. We are instead leaving it up to each
of the sub classes LectureCourse, LabCourse, and IndependentCourse to specify its own
class-appropriate way of performing the service.

Implementing Abstract Methods:

When we derive a class from an abstract super class, the sub clas will inherit all of the super
classs features, including the abstract methods.
To replace an inherited abstract method with a concrete version, the sub class needs to
merely override it; in doing so, we drop the abstract keyword from the method header and
replace the terminating semicolon with a method body, enclosed in braces.

Eg:
public class LectureCourse extends Courses
{
public void establishCourseSchedule(Strring startDate, String endDate)
{
/* Logic specific to the business rules for the LectureCourse
determine what day of the week the startDate falls on
determine how many weeks there are between startDate and the endDate;
schedule one 3hrs class meeting per week on the appropriate day of the
week
*/
}
}

Abstarct classes and Instantiation:

Why we should not create object for an abstract class?

Abstract classes cannot be instantiated.


i.e The Courses class is declared to be abstract, we cant ever instantiate generic Courses
objects in our application, an attempt to do so will result in a compilation error
Courses c=new Courses();
/* Error */
The Courses class declares an establishCourseSchedule() method header, thus implying that
Courses are able to perform this service by providing no body to explain how the method is
to be performed.
If we are able to instantiate a Courses object, it would therefore be expected to know how
to respond
c.establishCourseSchedule(02/24/2010,05/10/2010);
But there is no executable method body with the abstract establishCourseSchedule()
method, the Courses object in question wouldnt know how to behave in response to such a
call.
public abstract class A
{
public abstract void foo();
}

public class X public abstract class B


{ {
public void foo() public abstract void
{ bar();
Concrete Logic; }
}
}

public class Y
{
public void bar()
{
Concrete Logic;
}
public void foo()
{
Concrete Logic;
}
}

Program 1:

abstract class Abs_super


{
abstract public void display();
}
class Abs_demo extends Abs_super
{
public void display()
{
System.out.println("Executing abstract method");
}
public static void main(String s[])
{
Abs_demo obj = new Abs_demo();
obj.display();
System.out.println("Abstract Demo Successful");
}
}

Program 2:

abstract class A
{
abstract void call();
void callme()
{
System.out.println("THIS IS AN NON-ABSTRACT METHOD");
}
}

class B extends A
{
void call()
{
System.out.println("IMPLEMENTING A's ABSTRACT METHOD ");
}
}
public class absDemo
{
public static void main(String args[])
{
B b=new B();
b.call();
b.callme();
}
}

Você também pode gostar