Overriding, final , abstract
Overriding, final , abstract
Example:
class Bank
{
int getRateOfInterest()// super class method
{
return 0;
}
}
class Axis extends Bank// subclass of bank
{
int getRateOfInterest()// overriding the superclass method
{
return 6;
}
}
class ICICI extends Bank// subclass of Bank
{
int getRateOfInterest()// overriding the superclass method
{
return 15;
}
}
// Mainclass
class BankTest
{
public static void main(String[] a)
{
Axis a=new Axis();
ICICI i=new ICICI();
// following method call invokes the overridden method of subclass AXIS
System.out.println(“AXIS: Rate of Interest = “+a.getRateOfInterest());
// following method call invokes the overridden method of subclass ICICI
System.out.println(“ICICI: Rate of Interest = “+i.getRateOfInterest());
}
}
Output:
Z:\> java BankTest
AXIS: Rate of Interest = 6
ICICI: Rate of Interest = 15
class A {
void callme() {
System.out.println(“Inside A’s callme method”);
}
}
class B extends A {
//override callme()
void callme() {
System.out.println(“Inside B’s callme method”);
}}
class C extends A
{
//override callme()
void callme() {
System.out.println(“Inside C’s callme method”);
}
}
class Dispatch
{
public static void main(String args[])
{
In Method Overloading,
In Method Overriding, sub
Methods of the same class
class have the same method
shares the same name but each
with same name and exactly
method must have different
Definition the same number and type of
number of parameters or
parameters and same return
parameters having different
type as a super class.
types and order.
For example sending sms, you just type the text and send the message. You don't
know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Abstract Classes:
Example:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
Abstract Methods:
Write a Java program to create an abstract class named Shape that contains 2
integers and an empty method named PrintArea(). Provide 3 classes named
Rectangle, Triangle and Circle such that each one of the classes extends the
class Shape. Each one of the classes contain only the method PrintArea() that
prints the area of the given shape.
}
}
class Triangle extends shape
{
void printArea()
{
System.out.println("Area of Triangle is " + (x * y) / 2);
}
}
class Circle extends shape
{
void printArea()
{
System.out.println("Area of Circle is " + (22 * x * x) / 7);
}
}
class abs
{
public static void main(String[] args)
{
Rectangle r = new Rectangle();
r.x = 10;
r.y = 20;
r.printArea();
Output:
D:\>javac abs.java
D:\>java abs
Area of Rectangle is 200
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
Area of Triangle is 525
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
Area of Circle is 12
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
1. Final Variable:
Any variable either member variable or local variable (declared inside method or
block) modified by final keyword is called final variable.
The final variables are equivalent to const qualifier in C++ and #define directive
in C.
Syntax:
Example:
final int MAXMARKS=100;
final int PI=3.14;
There is a final variable speedlimit, we are going to change the value of this
variable, but It can't be changed because final variable once assigned a value can never
be changed.
1. class Bike
2. {
3. final int speedlimit=90;//final variable
4. void run( )
5. {
6. speedlimit=400;
7. }
8. public static void main(String args[])
9. {
10. Bike obj=new Bike();
11. obj.run();
12. }
13.}
Output: Compile Time Error
NOTE: Final variables are by default read-only.
2. Final Methods:
Final keyword in java can also be applied to methods.
A java method with final keyword is called final method and it cannot be
overridden in sub-class.
If a method is defined with final keyword, it cannot be overridden in the
subclass and its behaviour should remain constant in sub-classes.
Syntax:
final return_type function_name(parameter_list)
{
// method body
}
Example of final method in Java:
1. class Bike
2. {
3. final void run()
4. {
5. System.out.println("running");
6. }
7. }
8. class Honda extends Bike
9. {
10. void run()
11. {
12. System.out.println("running safely with 100kmph");
13. }
14. public static void main(String args[])
15. {
16. Honda honda= new Honda();
17. honda.run();
18. }
19.}
Output:
D:\>javac Honda.java
Honda.java:9: error: run() in Honda cannot override run() in Bike
void run()
^
overridden method is final
1 error
3. Final Classes:
Java class with final modifier is called final class in Java and they cannot
be sub-classed or inherited.
Syntax:
final class class_name
{
// body of the class
}
Several classes in Java are final e.g. String, Integer and other wrapper classes.
D:\>javac Honda.java
Honda.java:4: error: cannot inherit from final Bike class Honda extends Bike
^
1 error
Points to Remember: