After a long break for personal reasons I return to ; publication of the study notes for SCJP certification you want to see the next or previous chapters remember you can go to the index which lists all the Series Here
This time we will review the declaration of classes and the switches that we can use them
write code that is structured always in classes or interfaces (we will speak of packages) and within put variables and methods, the way we declare each of these elements will affect the behavior of our code.
For example to change a public method private to do this out of sight of all the other methods that reference it except in the case of methods belonging to the same class it was declared.
File Declaration Rules Code:
can be only one class with the modifier public per file
Comments may be at the start or end of any line of code
If in a public class file is a file name must be the same as that of the class, for example if we have a class file:
the file must be named Cat.java
order statements is as follows:
- package declaration import statements
- class declaration
both sentences package or import could not be present, but should be kept the same order, for example based on the previous example the following combinations are valid:
statements import package and apply to all classes within the code file and are declared only once at the beginning of the file, eg
imagine the following code within the file Cat.java
A file can have more than one class that has the public modifier but if that does not contain a public class does not need the same name as any of the classes inside
Class Declaration and Modifiers
enough to declare a class with a ruling class the following code is the minimum necessary to have a valid class:
However, we can add modifiers before the ruling class to modify the behavior of the same, these switches are 2 types:
modifiers access ( public, protected, private )
other switches ( strictfp, final, abstract )
Access Modifiers:
there anything interesting about access levels (also called access control or access control) is that there are 4, but we have only mentioned 3 access modifiers the fourth level of access control is the default access or package, then declare the class before:
has the level of packet access control, in other words, is that we place an access modifier or not, every class method or attribute already has an access level assigned
another point to consider is that although there are 4 levels of access control in a class you can enter only 2 these are:
- A ccess default or package (no modifier) \u200b\u200b
- A ccess public (public switch )
say that a class A has access to a class B means that:
- A can create an instance of B.
- A can extend B (that is, may become a subclass of B).
- A can access to certain methods and variables in B, depending on the control of access to such methods and variables.
When we talk about access classes we also talk about visibility, the level of access that exist between them takes precedence over the level of access that have their methods or attributes, that means that if class A has no access to the class B, no matter what level of access methods or attributes, the class can not access any method or attribute of B.
o Default Access Package
As mentioned this is the level of access that we get if we do not add any switches to a class at this level is also known as Access Level package because a class with default access level can only be seen by other classes that are within the same package as the same level (explain this in the example).
Examples:
file1:
file2:
here Carreta class with package access level is in a different package that the class Auto, and while class file compiles perfectly Carreta class file Auto tells us that "Auto not can be resolved to type "
get the same in the following case:
file1:
file2:
This time Auto class is in the vehicle package is a subpackage of antiques, and yet get the same error so we can concluded that the access level of the package is tight, no matter if the class inherits this in a subpackage.
To make the code work we have 2 options: Declare the class as public Carreta adding the modifier public or placing both classes in the same package as in the following example: file1
:
file2:
This third case is allowed access to the class from the class Car Carreta since both vehicles belong to the package
Public Access A class is publicly accessible if added before of the ruling class public Case with this we are doing this kind visible to all other classes in all packages, but remember that it is necessary even add the statement import to access the class from other packages such
based on the example of previous section for the statement to be validated using public access should change the class declaration by Carreta
file1:
file2:
Other Class Modifiers You
of the access switches are the switches we have seen: s trictfp, abstract and final , and can be used in conjunction with the access modifiers except the combination abstract and final. Switch
final
A class is final if we add to his statement switch final, a final class can not be subclassed, ie any class can inherit from final class, if you try to inherit from this class will get an error message at compile time, the reasons for this are varied but the principal is usually ensure that the implementation of a class remains unchanged, usually for security reasons.
Most classes that make up the core of the sdk are declared as final, for example the class String
should be noted however that much for example the following situation:
Imagine
not have the source code of a class that has not been declared final and need to make changes, you only need to inherit it, implement the necessary changes and calls to replace the old class for the new class you just modify If you need to change the class had been declared final it would not be possible and the change would be impossible to make
Example:
Continuing the example above now declare the class as final Carreta and see the message returned
file1:
file2:
if we tried to run the code would get the message: The type
Auto
Can not subclass final class the Carreta
abstract modifier
A class is abstract if we add to his statement abstract modifier , A abstract class can not be implemented, so that its sole purpose is to be subclassed or inherited by other classes this may be because this class will serve as the father of many classes or because it is too vague a concept to be structured , take for example a class called Animal is too vague as it represents all possible animals, how many legs does it have? how many eyes?, what color is it? ... more. and behave? it move?, walks, flies, no?, suddenly more than one at a time, as is born? of an egg, mother, etc. However
declaring once more specific classes of these questions can be answered as a Dog, Rabbit, etc.
Another fact to be noted is that the methods of an abstract class can also be abstract and therefore not be implemented, only declared. For this we include in the method declaration abstract switch and instead of the braces ({}) end in a semicolon (;) an abstract method has no implementation in this case the implement delegate who inherits the abstract class. An abstract class can have both abstract methods and non-abstract, but a non-abstract class can have abstract methods, in short: if a class contains at least one abstract method, the class must be declared as abstract. An example of non-abstract methods can have an abstract class are getters and setters for its attributes. Example
code above is correct and compiles without problems, however if we try to instantiate the class will get: Animal
class is an abstract class. It Can not Be instantiated.
This concludes the matter on the declaration of classes and access modifiers to them, in the next post will deal with the declaration of interfaces.
0 comments:
Post a Comment