You have three class modifiers in JAVA:
Can only be used in classes and methods.
If mark a class as strictfp you are saying that any method in the class will folow the IEEE 754 standard rules for floating points.
final
Marking the class with this keyword you are proibing the class to be subclassed.In pratice no one will be able to extend it.Why would you mark a class as final ?Sometimes you want things to be done "your way on no way" :)
Lets see what hapens when you try extend a class marked as final...
Declaring...
package cert;
public final class Beverage {
public void importantMethod() { }
}
Extending...
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage { }
Compiling...
Can't subclass final classes: class
cert.Beverage class Tea extends Beverage{
1 error
abstract
An abstract class cannot be instantiated.Its created you the purpose of being extended.Imagine class Vehicle there is no point in the creation of a instance of vehicle, but it makes a lot of sense in create a Car instance class that extends Vehicle providing Car with all common methods all vehicle have.
An abstract class cannot be instantiated.Its created you the purpose of being extended.Imagine class Vehicle there is no point in the creation of a instance of vehicle, but it makes a lot of sense in create a Car instance class that extends Vehicle providing Car with all common methods all vehicle have.
What no to do... :)
Creating a abstract Class
abstract class Car {
private double price;
private String model;
private String year;
public abstract void goFast();
public abstract void goUpHill();
public abstract void impressNeighbors();
// Additional, important, and serious code goes here
}
Trying to instantiate it...
AnotherClass.java:7: class Car is an abstract
class. It can't be instantiated.
Car x = new Car();
1 error
TO RETAIN:
- You can modify class declarations and use any of these modifiers in conjunction with any access control (public , default).
- You can't always mix these nonacces modifiers! You can't and there is no sense in making a class with final and abstract (i will talk about this later...but its like saying "This class can't and must be extended" it doen't make a lot of sense right ?:) )
- Abstract classes may have implemented methods, there are some methods you might consider give a "default" implementation, and so giving to the subclasses a common implementation.
- If a method is declared abstract you got to set the class as abstract