Mostrar mensagens com a etiqueta Java. Mostrar todas as mensagens
Mostrar mensagens com a etiqueta Java. Mostrar todas as mensagens

sexta-feira, 21 de outubro de 2011

JAVA - Class Modifiers


You have three class modifiers in JAVA:




strictfp

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.

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
Code retrived from here

Checkout this sites:


JAVA - Classes Access

Default
  • A class with default access as no modifier in its declaration
package certification;
class Beverage { }
  • Default access is package-level access, because a class with default access can be seen only by classes within the same package.This means if you try something like this:
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage { }
     You'll get ....
    Can't access class cert.Beverage. Class or interface must be
    public, in same package, or an accessible member class.
    import cert.Beverage;


    Public
    • A class with public access uses the public keyword.

    package certification;
    public class Beverage { }
    • Public access means that all classes can access and create subclasses based on public class.You still have to import the package but this time you want get that nasty error..
    Private
    • This can only be declared for inner classes, making the class private and only visible to the top level class where it is defined.
    Code retrived from here.

    quarta-feira, 19 de outubro de 2011