Java LAB 11

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Abstract Class,Interface and final keyword Lab # 11

LAB # 11

ABSTRACT CLASSES, INTERFACE AND


FINAL KEYWORD

OBJECTIVE:
To study Abstract classes, Interface and final keyword.

THEORY:

11.1 ABSTRACT CLASSES


There are situations in which you will want to define a superclass that
declares the structure of a given abstraction without providing a complete
implementation of every method. That is, sometimes you will want to create a
superclass that only defines a generalized form that will be shared by all of its
subclasses, leaving it to each subclass to fill in the details. Such a class
determines the nature of the methods that the subclasses must implement.

1. An abstract method will force to make the class abstract.


2. An abstract method cannot have a body. Only signature is defined with
";".
3. Abstract class can contain both abstract and non abstract methods.
4. To use the non abstract methods of abstract class we must make a
child class.
5. The child class must provide the implementation of the parent abstract
methods by overriding.
6. If a child cannot provide implementation of parent abstract methods the
abstract methods will force to make the child class abstract. In that
case we cannot make Object of child class.
7. Abstract class can be used as reference class but not as Object class.
8. After providing implementations of all abstract methods in the hierarchy
we can create Objects.

abstract class A
{
abstract void add()
{ //Abstract method with body will generate error
System.out.println("Add of A");
}

abstract void add();


abstract void sub();

Object Oriented Programming - OOPs 1


Abstract Class,Interface and final keyword Lab # 11

void mull()
{
System.out.println("Multiply of A");
}
void div()
{
System.out.println("Divide of A\n");
}
}

// Class B is abstract because of not providing the implementation of abstract


void sub() of A.

abstract class B extends A


{
void add()
{ //Class B providing implementation of abstract void add() of class A
System.out.println("Add of B");
}
void div()
{
System.out.println("Divide of B\n");
}
}

// Class C providing the implementation of abstract void sub() of A and


overriding multiply of A.
class C extends B
{
void sub()
{
System.out.println("Subtract of C");
}
void mull()
{
System.out.println("Multiply of C");
}

public class Abstract2


{
public static void main (String[] args)
{

//We can only make Object of class C


A obj1 = new C(); //Reference class A
B obj2 = new C(); //Reference class B
C obj3 = new C();

Object Oriented Programming - OOPs 2


Abstract Class,Interface and final keyword Lab # 11

obj1.add(); //Add of B
obj1.sub(); //Subtract of C
obj1.mull(); //Multiply of C
obj1.div(); //Divide of B

obj2.add(); //Add of B
obj2.sub(); //Subtract of C
obj2.mull(); //Multiply of C
obj2.div(); //Divide of B

obj3.add(); //Add of B
obj3.sub(); //Subtract of C
obj3.mull(); //Multiply of C
obj3.div(); //Divide of B
}
}

OUTPUT

10.2 INTERFACES

Using the keyword interface, you can fully abstract a class’ interface from its
implementation. That is, using interface, you can specify what a class must
do, but not how it does it. Interfaces are syntactically similar to classes, but
they lack instance variables, and their methods are declared without any
body. In practice, this means that you can define interfaces which don’t make
assumptions about how they are implemented. Once it is defined, any number
of classes can implement an interface. Also, one class can implement any
number of interfaces.

To implement an interface, a class must create the complete set of methods


defined by the interface. However, each class is free to determine the details
of its own implementation. By providing the interface keyword, Java allows

Object Oriented Programming - OOPs 3


Abstract Class,Interface and final keyword Lab # 11

you to fully utilize the “one interface, multiple methods” aspect of


polymorphism.

Program 1: Interface1
Interface X
(Abstract add and sub)

Class A Implements X
(Implements add and sub)

//Methods of Interface are by default abstract and public


//We can only define their signatures

interface X{
void add();
void sub();
}

//A child class implements (not extends) X interface


//A child class will provide implementation of both abstract methods
//Methods overriding will be used and access control must be public

class A implements X{
public void add(){
System.out.println("Add of A");
}
public void sub(){
System.out.println("Subtract of A");
}
}

public class Interface1{


public static void main (String[] args){
A obj1 = new A();
X obj2 = new A(); //Interface reference can be used to
perform //polymorphism

obj1.add ();
obj1.sub ();
System.out.println("\add and Subtract of A on Interface
reference\n");
obj2.add ();
obj2.sub ();
}
}

Object Oriented Programming - OOPs 4


Abstract Class,Interface and final keyword Lab # 11

OUTPUT

11.3 USING THE FINAL MODIFIER


You can use the final keyword to fix the value of a static data member of a
class. You can also apply this keyword to the definition of a method, and to
the definition of a class. It may be that you want to prevent a subclass from
overriding a method in your class. When this is the case, simply declare that
method as final. Any attempt to override a final method in a subclass will
result in the compiler flagging the new method as an error. For example, you
could declare the method addPoint() as final within the class PolyLine by
writing its definition in the class as:

public final void addPoint(Point point) {


ListPoint newEnd = new ListPoint(point);// Create a new ListPoint
end.setNext(newEnd); // Set next variable for old end as new end
end = newEnd; // Store new point as end
}

Any class derived from PolyLine would not be able to redefine this method.
Obviously, an abstract method cannot be declared as final — because it must be
defined in a subclass somewhere. If you declare a class as final, you prevent any
subclasses from being derived from it. To declare the class PolyLine as final, you
would define it as:

public final class PolyLine {


// Definition as before... }

If you now attempt to define a class based on PolyLine, you will get an error message
from the compiler. An abstract class cannot be declared as final since this would
prevent the abstract methods in the class from ever being defined. Declaring a class as
final is a drastic step that prevents the functionality of the class being extended by
derivation, so you should be very sure that you want to do this.

Object Oriented Programming - OOPs 5


Abstract Class,Interface and final keyword Lab # 11

LAB TASK

1. Implement below hierarchal diagram in java using interfaces.

Interface Y
(Abstract mull)

Interface X Extends Y
(Abstract add and sub)

Class A Implements X
(Implements add, sub and mull)

2. Create an Interface Shape with three child classes Circle, Square and
Rectangle. Create a method Draw in each class. Create an Object of
each class that can call the method Draw and it will call automatically
the Draw method of each class. The text of Draw method e.g. Circle is
"This is a Circle".

3. Define an abstract base class Shape that includes protected data


members for the (x, y) position of a shape, a public method to move a
shape, and a public abstract method show() to output a shape. Derive
subclasses for lines, circles, and rectangles. Also, define the class
PolyLine as its base class. You can represent a line as two points, a
circle as a center and a radius, and a rectangle as two points on
diagonally opposite corners. Implement the toString() method for each
class. Test the classes by selecting ten random objects of the derived
classes, and then invoking the show() method for each. Use the
toString() methods in the derived classes.

Object Oriented Programming - OOPs 6

You might also like