by Dinesh Thakur Category: Classes

A nested class as the name suggests is a class that is defined inside another class. It is merely a convenient way of grouping two or more classes together into a single unit or module. A class that contains a nested class is known as the enclosing or outer class of the nested class.

 
by Dinesh Thakur Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

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 Category: Classes

Copy Constructor: 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. It takes the object of the class as a reference to the parameters. This means that whenever we initialize an instance using value of another instance of same type, a copy constructor is used.

 
by Dinesh Thakur Category: Classes

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 Category: Classes

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 Category: Classes

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.

 

Page 2 of 3



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles