Professional Documents
Culture Documents
Objective:
To implement the concept of Abstract classes, Interface and Inner classes
Scope:
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract
classes cannot be instantiated, but they can be sub classes. When an abstract class is sub-classes, the sub-
class usually provides implementations for all of the abstract methods in its parent class.
Example 1: The Following program implement the concept of Abstract classes and method
Objective:
Scope:
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the
abstract methods of the interface. An interface is not a class. Writing an interface is similar to writing a
class, but they are two different concepts. A class describes the attributes and behaviors of an object. An
interface contains behaviors that a class implements.
Objective:
To implements the concepts of Inner classes.
Scope:
Inner classes nest within other classes. A normal class is a direct member of a package. Static member
class is a static member of a class. Like any other static method, a static member class has access to all
static methods of the parent, or top-level, class. Like a static member class, a member class is also defined
as a member of a class. Unlike the static variety, the member class is instance specific and has access to
any and all methods and members; even the parent's this reference. Local classes are declared within a
block of code and are visible only within that block, just as any other method variable.
Exercises:
Exercise 1:
Create an abstract class with at least one method, in its own package. Create a class in a separate package. Add
a protected inner class that implements the interface. In a third package, inherit from your class and, inside a
method, return an object of the protected inner class, up-casting to the abstract class during the return.
Exercise 2:
Create an interface with at least one method, and implement that interface by defining an inner class with-
in a method, which returns a reference to your interface.
Exercise 3:
Create three interfaces, each with two methods. Inherit a new interface from the three, adding a new
method. Create a class by implementing the new interface and also inheriting from a concrete class. Now
write four methods, each of which takes one of the four interfaces as an argument. In main( ), create an
object of your class and pass it to each of the methods.