Abstract Class
Abstract Class
Rule:
• If you are extending an abstract class that has an
abstract method, you must either provide the
implementation of the method or make this class
abstract.
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1
{
public static void main(String args[])
{
Shape s= new Circle1();
s.draw();
} }
Final Keyword In Java
• The final keyword in java is used to restrict
the user.
• Final can be:
– variable
– method
– Class
1) Java final variable
• If you make any variable as final, you cannot
change the value of final variable (It will be
constant).
Output:Compile Time Error
class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}
2) Java final method
• If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
double -> float -> long -> int -> char -> short -> byte
long l = (long)d;
int i = (int)l;