Saturday, May 30, 2015

final Keyword


// It prevents its content from being modified. Final field must be initialized when it is declared.
// A class can also be declared as final. A class declared as final cannot be inherited.
// String class in java.lang package is a example of final class.
// Method declared as final can be inherited but you cannot override(redefine) it.
// If you used this once the value  can't be changed again.
// This can be used to classes, methods, objects and variables.

// Final Methods
// Final methods cannot be overridden in other sub classes.
// It means the behavior of that method cannot be changed.

// Final classes
// Final classes cannot be extended.
// It is a final segment of class hierarchy of the program.

public class StudentInfo {

final int symbolNo = 130200 ;

final void studInfoMethod() {
System.out.println("Symbol no from studentInfo class: "+symbolNo);
}

public static void main(String[] args) {
StudentInfo obj = new StudentInfo();
obj.studInfoMethod();
}

}


----------------------------------------------------------------------------------------------------------


public class BooksInfo extends StudentInfo{
public static void main(String[] args) {
BooksInfo obj = new BooksInfo();
obj.studInfoMethod();
System.out.println(obj.symbolNo);
}
}


No comments:

Post a Comment