UNIT-2 Notes
UNIT-2 Notes
Classes and Objects: Introduction, Class Declaration and Modifiers, Class Members,
Declaration of Class Objects, Assigning One Object to Another, Access Control for Class
Members, Accessing Private Members of Class, Constructor Methods for Class, Overloaded
Constructor Methods, Nested Classes, Final Class and Methods, Passing Arguments by Value
and by Reference, Keyword this.
Methods: Introduction, Defining Methods, Overloaded Methods, Overloaded Constructor
Methods, Class Objects as Parameters in Methods, Access Control, Recursive Methods,
Nesting of Methods, Overriding Methods, Attributes Final and Static.
Example:
The class name starts with an upper-case letter, whereas variable names may start with
lower-case letters.
In the case of names consisting of two or more words as in MyFarm, the other words
for with a capital letter for both classes and variables.
In multiword identifiers, there is no blank space between the words.
The class names should be simple and descriptive.
Class names are nouns.
For example, it could include names such as vehicles, books, and symbols.
Acronyms and abbreviations should be avoided.
Class Modifier
Class modifiers are used to control the access to class and its inheritance characteristics.
Java consists of packages and the packages consist of sub-packages and classes
Packages can also be used to control the accessibility of a class
These modifiers can be grouped as (a) access modifiers and (b) non-access modifiers.
Example of class Declarations
1. A class without a modifier is declared with keyword class followed by name/identifier of
class, and name is followed by a pair of braces { }.
package pack1;
class Myclass{
/* class body */
}
A class must belong to a package. If a programming does not specify a package, the class is
placed in the default package generated by a compiler.
2. A class with modifier is declared as follows
package pack1;
public class Myclass{
/* class body */
}
This class is visible to all other classes in any package.
3. The modifier class with private and protected is used only for nested class.
package pack1;
class X{
Statements;
private class Myclass
{ // Class body
}
}
A private class is declared inside another class. Therefore, this class is visible to other member
of the enveloping class in which it is declared. For all other classes, this class is not visible.
4. Class with the final modifier are declared as follows
package pack1;
final class Myclass{
// Class body
}
A class with the modifier final cannot be extended that is cannot have subclasses but it can
have a super class.
5. Class with the abstract modifier are declared as follows
package pack1;
abstract class Myclass{
abstract void display();
}
A class declared abstract must have one or more abstract methods as its members that is
methods without a body; it is only the header of a method followed by semicolon.
Concept-3 Class Members
The class members are declared in the body of a class.
The class member may contain fields (variables in a class), methods, nested classes, and
interfaces.
The member of a class contains the members declared in the class as well as the members
inherited from a super class.
The scope of all the members extends to the entire class body.
There are two type of variable
1.Non Static variables: These include instance and local variables and vary in scope and value.
(a) Instance variables: These variables are individual to an object and an object keeps a copy
of these variables in its memory.
(b) Local variables: These are local in scope and not accessible outside their scope.
2. Class variables (Static Variables): These variables are also contains as static keyword. The
values of these variables are common to all the objects of the class.
Program:-
Concept-4 Declaration of Class Objects
Creating an object is also referred to as instantiating an object.
Objects in java are created dynamically using the new operator.
The new operator creates an object of the specified class and returns a reference to that
object.
Syntax: class_name object_name;
For Example A Class defined as
public class Farm{
int length;
int width;
}
An Object of class Farm may be declared as null
Farm myFarm;
Here, the variable myFarm simply refers to an object of Farm class whose value at present is
null;
The new operator creates a new object or an instance of a class. With this, the objects are
created.
The memory is allocated dynamically in the heap at runtime of the program.
Object_name = new className();
Here, myFarm is a variable of the type Farm. Each object of the Farm class has allocated
memory that can hold the values of instance variables length and width.
Definition/Creation of object
Program:-
Output:-
Output:-
Program:-
Parameterized constructor
A constructor which has a specific number of parameters is called a parameterized
constructor.
The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.
Arguments can be passed to the constructor in order to initialize the instance variable of an
object.
It has a mechanism for automatically initializing the values for objects as soon as the object
is created. This mechanism is called as constructors.
Program:-
Concept-9 Final Class and Methods
Final Class
A class that is declared with the final keyword is known as the final class.
A final class can’t be inherited by subclasses.
By use of the final class, we can restrict the inheritance of class.
In java, all the wrapper classes are final class like String, Integer, etc.
If we try to inherit a final class, then the compiler throws an error at compilation time.
We can create a class as a final class only if it is complete in nature it means it must not be
an abstract class.
Syntax:
accessModifier final class className
{
// Body of class
}
Program:-
Final Method
We can declare a method as final, once you declare a method final it cannot be overridden.
So, you cannot modify a final method from a sub class.
The main intention of making a method final would be that the content of the method should
not be changed by any outsider.
Syntax:-
final accessmodifier datatype methodname(Parameter….){
// Method body
}
Program:-
Concept-10 Passing Arguments by Value and by Reference
Call by Value:
Call by Value means calling a method with a parameter as value. Through this, the argument
value is passed to the parameter.
In call by value, the modification done to the parameter passed does not reflect in the caller's
function.
Any modification done in called function will not affect the original value. i.e., actual and
formal parameters are different
Program: Output:-
Call by Reference
Call by Reference means calling a method with a parameter as a reference. Through this, the
argument reference is passed to the parameter.
In call by reference the object will be passed to the method as an argument.
In the call by reference, the modifications done to the parameter passed are persistent and
changes are reflected in the caller's function.
That means any occurs in actual arguments will be reflected in the formal arguments.
Program: Output:-
Output:-
Concept-14 Class Objects as Parameters in Methods
Object as an argument is use to establish communication between two or more objects
of same class.
Objects can be passed as parameters to the Methods just like primitive data types.
It is called as Call by Reference.
When a primitive type is passed to a method, it is done by use of call-by-value.
Objects are implicitly passed by use of call-by-reference.
This means when we pass primitive data types to method it will pass only values to
function parameters so any change made in parameter will not affect the value of
actual parameters.
Program:-
Output:-
Concept-15 Recursive Methods
Recursion in java is a process in which a method calls itself continuously. A method in
java that calls itself is called recursive method.
Example:-
Program:-
Output:-
Concept-16 Nesting of Methods
A method of a class can be called only by an object of that class using the dot operator
(.) So, there is an exception to this.
A method can be called by using only its name by another method of the same
class that is called Nesting of Method.
Program:-
Concept-17 Overriding Methods
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
1. Method overriding is used for runtime polymorphism
Output:-
Important Question
1. Explain the concept of call by value and call by reference with an example.
2. Explain the concept of Nesting of Methods with an example.
3. Discuss the significance of overriding methods with a program.
4. Write a Java method to find the minimum values in given two values
5. Explain the need for recursive methods in Java with an example.
6. Explain the difference between method overloading and constructor overloading with
examples.
7. What access modifiers may be used with in a class? How do you access a methods
declared private in a class?
8. What is the constructor methods in a Java explain with an example program?
9. Write a short note on this keyword in Java?
10. Explain about final class and final method in Java?
11. What is a class and object? How to declared and class and object in Java with an
example program?
12. Explain about Assigning One Object to Another object in Java with an example?
13. Explain about class member of class in Java?