javamod3
javamod3
● In Java the data items are called fields and the functions are called methods.
● It provides a thought of template for an object and behaves like a basic data type
such as int.
Defining a Class:
● A class is a user defined data type with a templates that serves to define its
properties.
● Once the class type has been defined we can create variables of the type using
declarations that are similar to the basic data type declarations.
● In Java this variables are termed as instances of classes which are the actual
objects.
The basic form of a class definition is:
● Because the body is empty this class does not contain any properties and their
for cannot do anything, we can however compile it and even create objects using
it.
● The keyword extends indicate that the properties of the superclass name class
are extended to the classname class. This concept is known as inheritance.
Fields Declaration:
● Data is encapsulated in a class by placing the data fields inside the body of the
class definition.
● These variables are called instance variables because they are created
whenever and object of the classes instantiated.
● We can declare an instance variables exactly the same way as we declare local
variables.
METHODS DECLARATION:
● Methods are declared inside the body of the class but immediately after the
declaration of instance variables.
● The type specify is the type of value the method would return.
● This could be a simple data type such as int as well as any class type.
● It could even be void type if the method does not return any value.
● The list contains variable names and types of all the values we want to give to
the method as input.
● In the case where no input data are required the declaration must retain the
empty parentheses.
Example:
● Note that the method has a return type of void because it does not return any
value.
● We pass two integer values to the method which are then assign to the instance
variables length and width.
● We can directly use the length and width inside the method.
Assume that we want to compute the area of rectangle Defined by the class. This can
be done as follows:
● The new method rectArea() computes area of the rectangle and returns the
result.
● Since the result would be an integer the return type of the method has been
specified as it.
● An object in Java is essentially a block of memory that contains Space to store all
the instance variables.
● The new operator create an object of the specified class and returns the
reference to the object.
● Since we are outside the class we cannot access the instance variables and the
methods directly.
● To do this we must use the concerned object and the dot operator as shown
below:
CONSTRUCTORS:
● Java supports a special type of method called a constructor that enables and
object to initialize itself when it is created.
● Secondly they do not specify a return type not even void.This is because they
return the instance of the class itself.
METHOD OVERLOADING:
● In Java it is possible to create methods that have the same name but different
parameter list and different definitions. This is called method overloading.
● Method overloading is used when objects are required to perform similar task but
using different input parameters.
● When we call a method in an object ,java matches of the method name first and
then the number and type of parameters to decide which one of the definitions to
execute this process is known as polymorphism.
● The members that are declared static as shown are called static members.
● Since these members are associated with the class itself rather than individual
objects the static variables and static methods are often referred to as class
variables and class methods in order to distinguish them from there counterparts
,instance variables and instance methods.
● Static variables are used when we want to have a variable common to all
instances of a class.
● Java create only one copy for a static variable which can be used even if the
class is never actually instantiated.
NESTING OF METHODS:
● A Method can be called by using only its name by another method of the same
class this is known as nesting of methods.
INHERITANCE : EXTENDING A CLASS
● Java classes can be reused in several ways.
● This is basically done by creating new classes , reusing the properties of existing
ones.
● The mechanism of deriving a new class from an old one is called inheritance.
● The old class is known as the base class or super class or parent class and the
new one is called the sub class or derived class or Child class.
● The inheritance allows subclasses to inherit all the variables and methods of their
parent classes .
● The sub class will now contain its own variables and methods as well as those of
the super class.
OVERRIDING METHODS:
● There may be occasions when we want an object to respond to the same method
but have different behaviour when that method is called.
● This is possible by defining a method in the subclass that has the same name ,
same arguments and same return type as a method in the super class.
● Then when that method is called , the method defined in the sub classes invoked
and executed instead of the one in the super class this is known as overriding.
FINAL VARIABLES AND METHODS:
● If we wish to prevent the subclasses from overriding the members of the super
class, declare them as final using the keyword final as a modifier.
● Making a method final ensures that the functionality defined in this method will
never be altered in anyway.
● Final variables behave like class variables and they do not take any space on
individual objects of the class.
FINAL CLASSES
FINALIZER METHODS:
● The garbage collector cannot free the object resources such as file descriptors or
window system fonts.
● This is done using the modifier keyword abstract in the method definition.
VISIBILITY CONTROL
public access
when We declare a variable as public , that is made visible to all the classes outside the
class
friendly access
● The difference between the public access and the friendly access is that the
public modifier makes fields visible in all classes regardless of their packages
while the friendly access makes fields visible only in the same package but not in
other packages
protected access
● The visibility level of a protected field lies in between the public access and the
friendly access.
● That is the protected modifier makes the fields visible not only to all classes and
subclasses in the same package but also to subclasses in other package.
private access
// Using constructor
Integer num1 = new Integer(10); // Wrapping primitive int 10
output