by Dinesh Thakur

Java provides four levels of protection for methods and instance variables: public, private, protected and package. Before applying protection levels to a program, one should know what each form means and understand the fundamental relationships that a method or variable within a class can have to the other classes in the system.



 
by Dinesh Thakur

Super-class constructors cannot be overridden as the constructors have the same name as their class. To be able to access a constructor in a sub-class with the same number and data type of arguments as in the super-class, it must be defined in the sub-class itself. When constructors are defined in a sub-class, the corresponding super-class constructors are called during the creation of objects of that sub-class. This ensures that the initialization of inherited parts of the objects takes place similar to the way the super-class initializes its objects. Thus, defining constructors explicitly in the sub-class will override or overload super-class constructors.



 
by Dinesh Thakur

Constructor methods initialize new objects when they are created. Unlike regular methods, constructor methods cannot be called directly. They are called automatically when a new object is created. When an object is created in Java using the keyword new the following things happen:



 
by Dinesh Thakur

Apart from class and instance variables, Java also has class and instance methods. The differences between the two types of method are analogous to the differences between class and instance variables. Class methods are available to any instance of the class itself and can be made available to other classes. Therefore, some class methods can be used anywhere, regardless of whether an instance of the class exists or not.



 
by Dinesh Thakur

There are mainly two ways of passing arguments to methods:

• Pass by value

• Pass by reference

Java directly supports passing by value; however, passing by reference will be accessible through reference objects.



 
by Dinesh Thakur

Classes are the fundamental building blocks of any object-oriented language.

A class describes the data and behaviour associated with instances of that class. When a class is instantiated, an object is created: this object has properties and behaviour similar to other instances of the same class. The data associated with a class or object is stored in variables. The behaviour associated with a class or object is implemented by means of methods. Methods are similar to the functions or procedures of procedural languages such as C or Pascal.

 
by Dinesh Thakur

• Code reusability New objects can be derived from old objects, allowing for improvement and refinement of the code at each stage and also preserving parts of the code for other programs. This is used to develop many class libraries using class codes that have already been written, for example, Microsoft Foundation Classes (MFC).

 
by Dinesh Thakur

Now that we know the basic concepts in OOP, we are in a position to compare it with classical procedure oriented programming.

 
by Dinesh Thakur

In early days, programs were collections of procedures acting on data. A procedure is defined as a collection of instructions executed in sequential order. Data were independent of the procedures and programmers have to keep track of functions and the way they modify data. Structured programming is a simpler way to tackle this situation.

 
by Dinesh Thakur

Objects and classes are the building blocks of OOP. To understand OOP, first we have to know what objects and classes are.

 
by Dinesh Thakur

These are non static members classes. Whereas a non static inner classes are defined as instance members of other classes just like instance variable and method members are defined in a class. An instance of non static inner class always has an instance of the enclosing class associated with it.

 
by Dinesh Thakur

Nested class is also defined as a static member in a top level class. And the nested class can be instantiated using its full name and no instance of the enclosing class is required to instantiate a top level nested class.

 
by Dinesh Thakur

Constructor Overloading means a class having multiple constructors with the same name, but with different number of arguments or different type of arguments. The compiler differentiates the constructors based on the number of parameters in the list and their types. That means, on the basis of the number and type of the arguments that we pass into the constructor, the compiler determines which constructor to call.

 
by Dinesh Thakur

Constructors are the methods having the same name as that of the class and are automatically executed on defining an object. The main purpose of a constructor is to initialize a new object.

Java does not allocates memory for objects at application startup time but rather when the instance is created by keyword new. When new is invoked, Java allocates enough memory to hold the object and then initializes any instance variables to default values.



 
by Dinesh Thakur

A DATA STRUCTURE which is accompanied by a set of ACCESS FUNCTIONS that must be employed to create objects of that type and access their contents, without the programmer being concerned with the internal layout of the data structure. The CLASSES employed in OBJECT-ORIENTED PROGRAMMING are abstract data types whose concealment is actually enforced by the language syntax, but abstract data types may be created in conventional languages such as C, PASCAL and MODULA-2 too, where the concealment is voluntary.



 
by Dinesh Thakur

In this Java Example we declares a class named Rect. It contains two integer member variables, l and b (for length and breadth). Since no access specifier is used, these variables are considered public. Public means that any other class can freely access these two members.



 
by Dinesh Thakur

Whenever the objects of a class are instantiated, each object will have its own copy of instance variable(s), however all objects share only one copy of each instance method of the class. So the question arises how does a method know for which object the method was called. In other words, how does a method know which instance variable it should get/set or use to calculate a value? This is possible using a special object reference variable named this. When a method starts executing, the JVM sets the object reference this to refer to the object for which the method has been invoked. The compiler uses this implicitly when your method refers to an instance variable of the class. So any instance variable referred to by a method is considered to this. instanceVariable.

 
by Dinesh Thakur

A method can return an object in a similar manner as that of returning a variable of primitive types from methods. When a method returns an object, the return type of the method is the name of the class to which the object belongs and the normal return statement in the method is used to return the object. This can be illustrated in the following program.

 
by Dinesh Thakur

In most of the programming languages (like C language), there are two ways of passing arguments to a method : Pass by Value and Pass by Reference.

 
by Dinesh Thakur

In Java, we have three access Specifiers: public, private and protected that can be used with the class members. If no access specifier is used, Java assigns a default package access to the members. Members with default access are accessible from methods in any class in the same package.



 
by Dinesh Thakur

Sometimes a programmer wants to create an exact but separate copy of an existing object so that subsequent changes to the copy should not alter the original or vice versa. This is made possible using the copy constructor.

 
by Dinesh Thakur

A constructor can call another constructor from the same class. This is usually done when a constructor duplicates some of the behavior of an existing constructor. In order to refer to another constructor in the same class, use this as the method name, followed by appropriate arguments enclosed in pair of parentheses, if necessary. Java requires that the call to this () can be used in a constructor and must be the first statement in the constructor followed by any other relevant code.



 
by Dinesh Thakur

We saw that a class had only one constructor with either zero, one or more parameters. The constructor is key for object initialization. The mechanism of constructor is made considerable more powerful by combining with the feature of overloading. Constructor can be overloaded in exactly the same way as you can overload methods.

 
by Dinesh Thakur

Unlike default constructor which do not have any parameters, it is however possible to have one or more parameters in a constructor. This type of constructor which have parameters is known as parameterized constructor. Using parameterized constructor, it is possible to initialize objects with different set of values at the time of their creation. These different set of values initialized to objects must be passed as arguments when constructor is invoked. The parameter list can be specified in the parentheses in the same way as parameter list is specified in the method.

 
by Dinesh Thakur

Each time an object is created, a Constructor is always invoked. In the previous program, the constructor we created was invoked. But in the programs that we made earlier, we did not create any constructor. So in such situations, the compiler automatically writes one for you. This constructor is known as the default constructor. It does not contain any parameters nor does it contain any statements in its body. Its only purpose is to enable you to create object of class type. The default constructor looks like

 
by Dinesh Thakur

Initializing a variable is considered very helpful while making programs. We can initialize variables of primitive types at the time of their declarations. For example:

 
by Dinesh Thakur

It is possible to have static methods in a class in the same way as we have static fields. The static method is similar to instance method) of a class but the only difference is that the static method can be called through its class name without creating any object of that class. A static method is also called class method as it is associated with a class and not with individual instance of the class. We have already seen the sqrt () method which is a static method within Java standard Math class. A method is declared static when its behavior is not dependent on the instance variable just like Math.sqrt (4.5).This method never uses instance variables so it does not need to know about the specific object.



 
by Dinesh Thakur

In the preceding sections, we have discussed that each object of a class will have its own copy of all the fields of the class. However, in certain situations, it may be required to share a common copy of fields among all the objects of the same class. This is accomplished by declaring the field(s) to be static and such fields are known as static field(s). If a field is declared static then there is one field for the entire class instead of one per object. A static field of a class is often referred to as a class variable because static field is associated with a class and not with individual instances of the class. A static field gets memory only once for the whole class no matter how many objects of a class are created. To declare a static field, prefix the field declaration in the class with the static modifier. Its syntax is,



 
by Dinesh Thakur

In this example, we compute the electricity bill of a particular customer. The class ElectricityBil1 contains field customerNo, name and units. It also contains methods setData () show () and billcalculate ().The statement,

 
by Dinesh Thakur

This example is used to calculate the area and circumference of a circle with given radius. The class Circle contains the field radius and methods setData (), area ()and circumference ().The statement.

 
by Dinesh Thakur

When you compile this program, you will find that two class files have been created, one for Rectangle and one for RectangleArea. The Java compiler automatically puts each class into its own class file.



 
by Dinesh Thakur

When multiple methods in the same class have the same name but differ in the number or types of parameters, it is known as Method Overloading. When an overloaded method is invoked, it is the responsibility of the compiler to select the appropriate overloaded method based on the number of argument(s) passed and if the numbers of argument(s) are same then depending upon the type of argument(s) passed to the method. Thus, the key to method overloading is a method's parameter list. A method's return type is not enough to distinguish between two overloaded methods. If the compiler detects two methods declarations with the same name and parameter list but different return types then it will generate an error.

 
by Dinesh Thakur

When values of built in types are passed as arguments to a function. It is known as Call by value. The changes made to formal parameters in the called function are not reflected in the actual arguments class.

 
by Dinesh Thakur

In java, base class reference can be assigned objects of sub class. When methods of sub class object are called through base class’s reference.



 
by Dinesh Thakur

Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.



 
by Dinesh Thakur

Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.



 
by Dinesh Thakur

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.



 
by Dinesh Thakur

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

 
by Dinesh Thakur

Wrapper Classes:-  We know that Vectors can’t handle Primitives data types like int, float ,char, long So that Primitives data type may be Converted into object data types by using wrapper classes those are contained in java.lang packages The Various Wrapper Classes are :-