You are on page 1of 3

1. Java, as an Object Orietated language Classes represent base elements of an application in the Java language.

It is known that a
class is an object's model. It contains internal fields and methods throught which it can interact with the object. Declaration of a class is similar to the declaration of a new type of data, it is described only how it looks and what it can do an object of that type (class), without creeating such an object. The Java language allows overloading of the methods. There can be defined more methods with the same name, which differ by signature (number and type of parameters). The existence of this mechanism is useful to define different behaviors when recieving the same message (name of method) depending on the (context) call. Because the Java language has been defined starting from C++, mostly all concepts relating to objects and classes are similar to those from C++. We can find the following notions : encapsulation, method overloading, inheritance and polymorphism. With the exception of values defined through primitive types, in Java language, anything else is considered an object. As in other oriented-object languages, all the clases form an hierarchy. The root of this hierarchy is given by the Object class. The Java language is Strong typed, which means any construction has a specific type set at the compiling, from this reason there aren't implicit types for methods and no variable number of arguments for calling the methods. If associating the call to a method with that method is done only in the running phase (with other words in the generated code by the compiler it is contained the name of the method, and at the running moment, it is searched in that interface a method that matches), it is said that the language has a dinaming binding. In the Java language, the binding is dinamic, which allows substituting an object with another one during runtime of the program, with the condition that the two object share the same interface. A class can be extended through redefining of some methods and/or adding some methods and new fields. Everything that isn't defined is inherited. A method that substitutes a method in overclass has the same name, the same type of outcome (or a subclass of this) and the same list of parameters. Otherwise, we would be talking of a new method. There can't be redefined the methods or fields that are declared with the final atribute. Introducing the concept of field of final method was necessary because of security, in this way the programmer can forbid redefining certain informations or methods considered critical. The class which is extended is called overclass (the base class). The obtained class through extension is called subclass (derived class). The Java language allows just simple inheritance: for each class there is only one overclass, all the classes have a comun ancestor: the Object class. For the situations in which in the C++ language we would have used multiple inheritance, in Java language it is utilised interfaces and simple inheritance. Utilising the class definition, there can be instantiations of objects having the behavior described by the class. At creation, the memory allocation takes place for fields and the processing indicated by the class constructor are done. The constructor is a methid that has exactly the same name with that class and is implicitely called when creating the object. The only context in which a constructor can be reffered (called as method) is from another constructor of

the same class, but this can be done only in certain special conditions. Like in C++, the constructors don't have outcome, and so there isn't specified the outcome type when defining them. The root of the class hierarchy in Java language, the Object class contains a series of final methods that will be inherited by any other class defined in Java. Some of the Object class methods aren't written in Java, because their implementation depend on the machine where the running takes place, as these methods are native. When a class is necessary (because a certain object of that type is being instantiated or because a static method is called from that class) this will load in the virtual machine. Loading is being done by an object called class loader. The Object class deffinition is contained the the java.lang library and it will be loaded from the machine that is running the program.

2. Constructors
When creating the object indicated by the pisica variable, it was considered that there weren't necessary special processing, properly it is considered that it is implicitely run only a constructor of the overclass (in our class a object of the Object type is created) and it is added current fields (nume and numarSoareci) at the built object. If when instantiating the object are neccesary specific processing of the current class (not overclass), there must be defined a constructor. A constructor is a special methid that is called when creating the object. Just like in C++, the method has the same name as the class that it belongs to and doesn't return any result. A constructor cannot be called from other methods, with the exception of other constructors (but using special notations). The first instruction from a constructor is implicit(in the way that it isn't necessary to be specified) a call of the constructor with no arguments of the superclass. The only deviation from this rule takes place when the first instruction from the constructor is an explicit call to another class constructor or to another superclass constructor. In this way, when constructing (instantiating) an object it is always begun, actually, with constructing an object of the Object type which is then extended acording to the class hierarchy. If in a class there isn't defined any constructor (just like in the first declarations of the Pisica class), it is considered that there is an constructor with no arguments (called implicit constructor) whose body contains only a call of the constructor, implicit of the superclass. If in a class it is defined at leas a constructor, the constructor with no arguments isn't supplied automatically. The constructors cannot have acces modifiers like abstract, native, static, synchronized or final. To refer to the object field, in a non-static method from a class it can be utilised the generical refferance this, which designate the current object. So, to reffer to the nume field in the body of a methid it can be used the name nume or the notation this.nume (just when because of defining a local variable of a parameter with the same name, the valability domain of that field is interupted). From a object there can be referanced variables from superclass, utilising the super.nume notation or referring explicitely the superclass name (in case of

the static variables). Utilising the notation based on this is necessary in other contextes, for example if it necessary to transmit the current object as an argument in invocation of a methid (eventually) for another object. In a constructor it can be called as a first instruction a constructor of the current class or a constructor of the class which the current class extends. It is the only position in the constructor definition that it can appear a call of another constructor. Such a call can appear under this way: this(Argumentlist); /// the constructor from the same calss, where Argumentlist repesent the list of parameter values of the invoked constructor

You might also like