Inheritance Concepts In Java

y)

{

dim1 = x;

dim2 = y;

}

double area()

{

System.out.println(“Area of figure undefined”);

return 0;

}

}

class Rectangle extends Figure

{

Rectangle(double x,double y)

{

super(x,y);

}

double area()

{

return dim1 * dim2;

}

}

class Triangle extends Figure

{

Triangle(double x,double y)

{

super(x,y);

}

double area()

{

return (float)1/2 * dim1 * dim2;

}

}

class FindArea

{

public static void main(String args[])

{

Figure    fig = new Figure(10,10);

Rectangle rec = new Rectangle(9,5);

Triangle  tri = new Triangle(10,8);

System.out.println(“area of figure is “+ fig.area());

System.out.println(“area of rectangle is “+ rec.area());

System.out.println(“area of triangle is “+ tri.area());

}

}

3  Introducing final key word

The key word final has three uses.

ü  First,it can be used to create the equivalent of a named constant.

ü  The other two uses of final apply to inheritance.

Both are examined here

3.1 Using final with variable

Declaring a variable , as final, prevents the contents of the variable from being modified.

Syntax:

final type variable-name = value;

For example

final int FILE_OPEN = 1;

final int FILE_CLOSE = 2;

final int FILE_SAVE = 3;

final int FILE_ SAVEAS = 4;

final int FILE_ QUIT = 5;

The variable declared as final can be directly used with in the program, FILE_OPEN ,  FILE_NEW,FILE_SAVE etc as if they were constants, with out fear that a value has been changed).
Remember

ü  You must initialize a variable declared as final, during declaration only( final is similar to const in c,c++).

ü  It is a coding convention to choose all upper case indentifiers for final variables.

3.2 Using final with inheritance

i) Using final to prevent over riding:

Though method over riding is java’s most powerful feature, there will be times when you will want to prevent it from occurring.To achieve this simply specify final as a modifier at the start of its declaration, consider the code fragment

//fragment of code demonstrating erroneous implementation of over riding

class A

{

final void meth()

{

System.out.println(“This is a final method”);

}

}

class B extends A

{

void meth()  //Error can’t over ride cause meth() method is already declared as final in class A

{

System.out.println(“This method wont be printed”);

}

}

/*

This program demonstrates practically that over loaded forms of methods are not

affected by methods declared as final.

*/

class A

{

final void meth()

{

System.out.println(“This is the final method”);

}

}

class B extends A

{

void meth()               //Not Ok, Error; cause This meth() is declared final in class A

{

System.out.println(“illegal and this wont work”);

}

void meth( int x){//This is ok because over loaded form of meth(int x) is

System.out.println(“u are in B and x = “+x);//not declared final in class A

}

}

class UseFinalMethod

{

public static void main(String args[])

{

B obj1 = new B();

obj1.meth(2);

}

}

Explanation:

Remember

ü   Only those methods, with same name and signature parameter , declared as final cannot be over rided but they can be over loaded. as demonstrated in the above example.

ü  If final method tried to be over ridden, compile time error will occur.

ii) Using FINAL to prevent Inheritance

Though Inheritance is java’s most powerful feature, there will be times when you will want a class, to be prevented, from being  inherited. To achieve this simply specify final as a modifier preceding the class declaration.

Thus to prevent a class from being further inherited, follow the general syntax shown below

final class class-name

{

// statements;

}

consider the code fragment,

final class A

{

//statements;

}

class B extends A // Error ! can’t sub class

{

//statements;

}

2.            Abstract Classes

Some times we would want to create a super class that only defines a generalized form , that will be common to all  of its, subclasses ‘(derivatives), leaving it to each sub class to fill

Pages: 1 2 3 4 5 6 7