Tuesday, November 15, 2011

Final Keyword and Example

/* All members and variables can be

overriden by default in subclasses.

Final Keyword Is Used in Java to

prevent the subclass from overridding

the members of the superclass. Making

a method final ensures that the

functionality defiened in the mathod

will never be altered in any way.

Similarily the value of the final

variable can never be altered in any

way. */
class fe
{
double fp;
int fr;

fe(double p1,int r1)
{
fp=p1;
fr=r1;
}
}

class se extends fe
{
double sp;
int sr;

se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp=p2;
sr=r2;
}
}

final class te extends se
{
double tp;
int tr;

te(double p1,int r1,double p2,int

r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp=p3;
tr=r3;
}

double average()
{
return (fp+sp+tp)/3;
}
}

class Student
{
public static void main(String args[])
{
te student = new

te(75.6,40,78.6,44,70.5,48);
System.out.println("Year\tRoll

no.\tPercentage");
System.out.println("FE\t"+student.fr+"

\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"

\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"

\t\t"+student.tp);
System.out.println("Average

performance is " + student.average());
}
}
/* Output *

Year Roll no. Percentage
FE 40 75.6
SE 44 78.6
TE 48 70.5
Average performance is

74.89999999999999 */

No comments:

Post a Comment