0% found this document useful (0 votes)
32 views16 pages

java module2

moduke 2

Uploaded by

ranjitha1065r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views16 pages

java module2

moduke 2

Uploaded by

ranjitha1065r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Module 2 The data, or variables, defined within a class are called instance variables. The
code is contained within methods. Collectively, the methods and variables
Introducing Classes: Class Fundamentals, Declaring Objects, Assigning defined within a class are called members of the class.
Object Reference Variables, Introducing Methods, Constructors, The this A Simple Class:
Keyword, Garbage Collection.
Methods and Classes: Overloading Methods, Objects as Parameters,
Argument Passing, Returning Objects, Recursion, Access Control,
Understanding static, Introducing final, Introducing Nested and Inner
Classes.
Chapter 6, 7
Chapter 6- Introducing Classes
As stated, a class defines a new type of data. In this case, the new data type
is called Box. You will use this name to declare objects of type Box.
he most important thing to understand about a class is that it defines a new
data type. Thus, a class is a template for an object, and an object is an instance
of a class.
A class is declared by use of the class keyword. x. A simplified general form
of a class definition is shown here:

As mentioned earlier, each time you create an instance of a class, you are
creating an object that contains its own copy of each instance variable defined
by the class.
Thus, every Box object will contain its own copies of the instance variables
width, height, and depth.
To access these variables, you will use the dot (.) operator. The dot operator
links the name of the object with the name of an instance variable. For
example, to assign the width variable of mybox the value 100, you would use
the following statement mybox.width=100;

Canara Engineering College Page 1 Canara Engineering College Page 2


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Declaring Objects
As just explained, when you create a class, you are creating a new data type.
You can use this type to declare objects of that type. First, you must declare a
variable of the class type. Second, you must acquire an actual, physical copy
of the object and assign it to that variable. You can do this using the new
operator. The new operator dynamically allocates memory for an object and
returns a reference to it. Let’s look at the details of this procedure. In the
preceding sample programs, a line similar to the following is used to declare
an object of type Box:
Box mybox = new Box();
This statement combines the two steps just described. It can be rewritten like
this to show each step more clearly:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object

Canara Engineering College Page 3 Canara Engineering College Page 4


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Adding a Method to the Box Class


Assigning Object Reference Variables
Object reference variables act differently than you might expect when an
Assignment takes place. For example,
Box b1 = new Box ();
Box b2 = b1;
You might think that b2 is being assigned a reference to a copy of the object
referred to by b1. Her both b1 and b2 refer to the same object.

Introducing Methods
Classes usually consist of two things: instance variables and methods.
Returning a Value
While the implementation of volume ( ) does move the computation of a box’s
volume inside the Box class where it belongs, it is not the best way to do it.
For example, what if another part of your program wanted to know the volume
of a box, but not display its value? A better way to implement volume ( ) is to
have it compute the volume of the box and return the result to the caller. The
following example, an improved version of the preceding program, does just
that.

Canara Engineering College Page 5 Canara Engineering College Page 6


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Here is an example:
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81
y = 2;
x = square(y); // x equals 4
Constructors
It can be tedious to initialize all of the variables in a class each time an instance
is created. Java allows objects to initialize themselves when they are created.
This automatic initialization is performed through the use of a constructor.
A constructor initializes an object immediately upon creation. It has the same
name as the class in which it resides and is syntactically similar to a method.
Once defined, the constructor is automatically called when the object is
created, before the new operator completes. Constructors have no return type,
not even void.

Adding a Method That Takes Parameters


While some methods don’t need parameters, most do. Parameters allow a
method to be generalized. That is, a parameterized method can operate on a
variety of data and/or be used in a number of slightly different situations. To
illustrate this point, let’s use a very simple example. Here is a method that
returns the square of the number 10: While this method does, indeed, return
the value of 10 squared, its use is very limited. However, if you modify the
method so that it takes a parameter, as shown next, then you can make square(
) much more useful. Now square( ) will return the square of whatever value it
is called with. That is, square( ) is now a general-purpose method that can
compute the square of any integer value, rather than just 10.

Canara Engineering College Page 7 Canara Engineering College Page 8


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Parameterized Constructors
While the Box( ) constructor in the preceding example does initialize a Box object, it is not
very useful—all boxes have the same dimensions. What is needed is a way to construct
Box objects of various dimensions. The easy solution is to add parameters to the
constructor. For example, the following version of Box defines a parameterized constructor
that sets the dimensions of a box as specified by those parameters. Pay special attention to
how Box objects are created.

Canara Engineering College Page 9 Canara Engineering College Page 10


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

The this Keyword : Garbage Collection


Sometimes a method will need to refer to the object that invoked it. To allow Since objects are dynamically allocated by using the new operator, you might
this, Java defines the this keyword. this can be used inside any method to refer be wondering how such objects are destroyed and their memory released for
to the current object. That is, this is always a reference to the object on which later reallocation. In some languages, such as traditional C++, dynamically
the method was invoked. You can use this anywhere a reference to an object allocated objects must be manually released by use of a delete operator. Java
of the current class type is permitted. takes a different approach; it handles deallocation for you automatically. The
To better understand what this refers to, consider the following version of technique that accomplishes this is called garbage collection. It works like
Box( ): this: when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.
There is no need to explicitly destroy objects. Garbage collection only occurs
sporadically (if at all) during the execution of your program. It will not occur
simply because one or more objects exist that are no longer used. Furthermore,
different Java run-time implementations will take varying approaches to
garbage collection.

Instance Variable Hiding :


As you know, it is illegal in Java to declare two local variables with the
same name inside the same or enclosing scopes. Interestingly, you can have
local variables, including formal parameters to methods, which overlap with
the names of the class’ instance variables.
However, when a local variable has the same name as an instance variable, the
local variable hides the instance variable. This is why width, height, and
depth were not used as the names of the parameters to the Box( ) constructor
inside the Box class.
If they had been, then width, for example, would have referred to the formal
parameter, hiding the instance variable width. While it is usually easier to
simply use different names, there is another way around this situation.

Because this lets you refer directly to the object, you can use it to resolve any
namespace collisions that might occur between instance variables and local
variables. For example, here is another version of Box( ), which uses width,
height, and depth for parameter names and then uses this to access the
instance variables by the same name:

Canara Engineering College Page 11 Canara Engineering College Page 12


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Chapter 7- Methods and Classes

Overloading Methods
In Java, it is possible to define two or more methods within the same class that
share the same name, as long as their parameter declarations are different.
When this is the case, the methods are said to be overloaded, and the process
is referred to as method overloading. Method overloading is one of the ways
that Java supports polymorphism.

When an overloaded method is invoked, Java uses the type and/or number of
arguments as its guide to determine which version of the overloaded method
to actually call. Thus, overloaded methods must differ in the type and/or
number of their parameters.

As you can see, test () is overloaded four times. The first version takes
no parameters, the second takes one integer parameter, the third takes
two integer parameters, and the fourth takes one double parameter. The
fact that the fourth version of test( ) also returns a value is of no
consequence relative to overloading, since return types do not play a role
in overload resolution.
Method overloading supports polymorphism because it is one way that
Java implements the “one interface, multiple methods” paradigm.

Overloading Constructors
In addition to overloading normal methods, you can also overload
constructor methods. To understand why, let’s return to the Box class
developed in the preceding chapter. Following is the latest version of
Box:

Canara Engineering College Page 13 Canara Engineering College Page 14


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

width= w;
class Box { height= h;
double width; depth= d;
double height;
double depth; }

// This is the constructor for Box // constructor used when no dimensions


Box(double w, double h, double d) specified Box() {
width= w; width= -1; // use -1 to indicate
height= h; height= -1; // an uninitialized
depth = d; depth= -1; // box

} }

// compute and return volume // constructor used when cube is created


double volume() { Box(double len) {
return width * height * depth; width= height= depth= len;
}
} }

As you can see, the Box( ) constructor requires three parameters. This // compute and return
volume double volume() {
means that all declarations of Box objects must pass three arguments to
return width* height* depth;
the Box( ) constructor. For example, the following statement is currently
invalid:
Box ob = new Box(); }
Since Box( ) requires three arguments, it’s an error to call it without them. }
This raises some important questions. What if you simply wanted a box
class OverloadCons {
and did not care (or know) what its initial dimensions were? Or, what if public static void main(String[) args) {
you want to be able to initialize a cube by specifying only one value that // create boxes using the various
would be used for all three dimensions? As the Box class is currently constructors Box myboxl = new Box(l0, 20,
written, these other options are not available to you. 15);
Box mybox2 = new Box();
* Here, Box defines three constructors to initialize Box mycube = new
the dimensions of a box various ways. Box(?);
*/
double vol;
class Box { double
width; double // get volume of first box
height; double vol = myboxl.volume();
depth;
System.out.println("Volume of myboxl is" + vol)
// constructor used when all dimensions specified
// get volume of second
Box(double w, double h, doubled) { box vol = mybox2.volume()
;
System.out.println("Volumeof mybox2 is II
+ vol)
Canara Engineering College Page 15 Canara Engineering College Page 16
OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

// get volume of cube As you can see, the equalTo( ) method inside Test compares two objects
vol = for equality and returns the result. That is, it compares the invoking
mycube.volume( ;) object with the one that it is passed. If they contain the same values, then
System.out.println("Volumeof mycube is II
+ vol)
}
the method returns true. Otherwise, it returns false. Notice that the
} parameter o in equalTo( ) specifies Test as its type. Although Test is a
class type created by the program, it is used in just the same way as Java’s
Using Objects as Parameters built-in types.
// Objects are passed through their references.
So far, we have only been using simple types as parameters to methods.
However, it is both correct and common to pass objects to methods. For class Test {
example, consider the following short program: int a, b;

/ Objects may be passed to methods. Test(int i, int j) {


class Test a= i;
{ int a, b = j;
b;
}
Test(int i, int j) {
a= // pass an object
i; void meth(Test o)
b {
= o . a * = 2;
j;
o.b I= 2;

} }
}
// return true if o is equal to the invoking object
class PassObjRef {
boolean equalTo(Test o) {
public static void main(String[] args) {
if(o .a == a && o . b == b ) return true; Test ob= new Test(lS, 20);
else return false;
System.out.println(11ob.a and ob.b before call: 11
+
} ob.a + " " + ob.b);
}
ob.meth(ob);
class PassOb {
public static void main(String[) args) System.out.println(11ob.a and ob.b after call: 11
+
{ Test obl = new Test(l00, 22); ob.a + " 11 + ob.b);
Test ob2 = new Test(l00, 22); }
Test ob3 = new Test(-1, -1) ; }
Sys tem .ou t.prin tln( "obl -- ob2: " +
obl.equa1To(ob2)) System.out.println("obl -- ob3: This program generates the following output:
ob.a and ob.b before call: 15 20
" + obl.equa1To(ob3 )) ob.a and ob.b after call: 30 10
}
}
Canara Engineering College Page 17 Canara Engineering College Page 18
OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

Returning Objects
A method can return any type of data, including class types that you
create. For example, in the following program, the incrByTen( ) method
returns an object in which the value of a is ten greater than it is in the
invoking object.
The
/ Returning an object .
class Test {
int a;

Test(int i) {
a= i;

Test incrByTen() {
Test temp = new Test(a+l0);
return temp;

}
}

class RetOb {
public static void main(String[l args) {
Test obl = new Test(2);
Test ob2;

ob2 = obl.incrByTen();
System.out.println("obl.a: " + obl.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob.2a after second
increase:
+ ob2.a);
}
}

The output generated by this program is shown here:


ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22

Canara Engineering College Page 19 Canara Engineering College Page 20


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

implement in an iterative way. Also, some types of AI-related algorithms


are most easily implemented using recursive solutions.
Recursion
Java supports recursion. Recursion is the process of defining something in
terms of itself. As it relates to Java programming, recursion is the attribute that
class RecTest {
allows a method to call itself. A method that calls itself is said to be recursive.
int[] values ;
The classic example of recursion is the computation of the factorial of a
number.
RecTest(int i) {
/ A simple example of recursion. values= new
class Factorial { int[i];
// this is a recursive
method int fact(int n) {
}
int result;

// disp lay array --


if(n==l) return l;
recursively void
result= fact(n-1) * printArray(int i) {
n; return result; if(i==O) return;
} else printArray(i-1);
} System.out.println("[" + (i-1) + "] "+ values[i-
1])
class Recursion {
public static void main(String[) args) { Factorial f }
= new Factorial(); }

" + f.fact(3)) class Recursion2 {


System.out .pr intln("Factorial of 3 is public static void main(String[J
System.out.println("Factorial of 4 is args) { RecTest ob= new
" + f.fact(4)) RecTest(lO);
inti;
System.out.println("Factorial of 5 is " +
f.fact(5)) for(i=O; i<lO; i++) ob.values[i] = i;
}
}
ob.printArray(lO)
;
}
The output from this program is shown here: }
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120 This program generates the following output:
[0] 0
The main advantage to recursive methods is that they can be used to [1] 1
[2] 2
create clearer and simpler versions of several algorithms than can their [3] 3
iterative relatives. For example, the QuickSort sorting algorithm is quite [4] 4
difficult to [5] 5
[6] 6
[7] 7
Canara Engineering College Page 21 Canara Engineering College Page 22
OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

[8] 8
[9] 9
// This is not OK and will cause an error
II ob.c = 100; II Error!
Introducing Access Control
As you know, encapsulation links data with the code that manipulates it. // You must access c through its methods
ob.setc(l00) ; II OK
However, encapsulation provides another important attribute: access
System.out.println("a, b, and c: " + ob.a + " " +
Control Java’s access modifiers are public, private, and protected. Java ob.b + " " + ob.getc());
also defines a default access level. protected applies only when
inheritance is involved. The other access modifiers are described next.
}
Let’s begin by defining public and private. When a member of a class
}
is modified by public, then that member can be accessed by any other
code.
As you can see, inside the Test class, a uses default access, which for
When a member of a class is specified as private, then that member can
this example is the same as specifying public. b is explicitly specified as
only be accessed by other members of its class. Now you can understand
public.
why main( ) has always been preceded by the public modifier. It is called
Member c is given private access. This means that it cannot be accessed
by code that is outside the program
by code outside of its class. So, inside the AccessTest class, c cannot be
* This program demonstrates the difference between used directly. It must be accessed through its public methods: setc( ) and
public and private. getc( ). If you were to remove the comment symbol from the beginning
of the following line,
I
class Test { / This class defines an integer stack that can hold 10
value
int a; // default access class Stack {
public int b; // public access
private int c; // private access /* Now, both stck and tos are privat.
e This means
// methods to access c
that they cannot be accidentally or
void setc(int i) { // set e's value maliciously altered in a way that would be
C = i; harmful to the stack.
*/
}
int getc() { // get e's value
private int[) stck = new
return c; int[l0); private int tos;
} // Initialize top-of-
} stack Stack() {
tos = -1;
class AccessTest {
public static void main(String[J
args) { Test ob= new Test(); // Push an item onto the
stack void push(int item) {
if(tOS==9)
// These are OK, a and b may be accessed directly System.out.println(''Stack is full.");
ob.a = 10 ; else
ob .b = 20; stck[++tos] = item;

Canara Engineering College Page 23 Canara Engineering College Page 24


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

// Pop an item from the


stack int pop() {
if(tos < 0) {
System.out.println("Stack
underflo.w"); return 0;

}
else
return stck[tos--];

class TestStack {
public static void main(String[J
args) { Stack mystackl = new
Stack();
Stack mystack2 = new Stack();

// push some numbers onto the stack


for(int i=0; i<l0; i++)
mystackl.push(i); for(int i=l0; i<20;
i++) mystack2.push(i)

// pop those numbers off the stack


System.out.println("Stackin
mystackl:"); for(int i=0; i<l0; i++)
System.out.println(mystackl.pop(; ))

Syste m.out.println("Stack in

mystack2
:"); for(int i=0; i<l0; i++)
System.out.println(mystack2.pop());

// these statements are not legal


// mystackl.tos = -2;
// mystack2.stck[3) = 100;

}
}

Canara Engineering College Page 25 Canara Engineering College Page 26


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

}
Understanding static
There will be times when you will want to define a class member that As soon as the UseStatic class is loaded, all of the static statements are
will be used independently of any object of that class .To create such a run. First, a is set to 3, then the static block executes, which prints a
member, precede its declaration with the keyword static. When a message and then initializes b to a*4 or 12. Then main( ) is called, which
member is declared static, it can be accessed before any objects of its calls meth(), passing 42 to x. The three println( ) statements refer to the
class are created, and without reference to any object. You can declare two static variables a and b, as well as to the parameter x.
both methods and variables to be static. The most common example of a Here is the output of the program:
static member is main(). main( ) is declared as static because it must be Static block initialized.
x = 42
called before any objects exist. a = 3
Instance variables declared as static are, essentially, global variables. b = 12
When objects of its class are declared, no copy of a static variable is Here is an example. Inside main( ), the static method callme( ) and the
made. Instead, all instances of the class share the same static variable. static variable b are accessed through their class name StaticDem

Methods declared as static have several restrictions: class StaticDemo {


static int a= 42;
• They can only directly call other static methods of their class. static int b = 99;
• They can only directly access static variables of their class.
• They cannot refer to this or super in any way. static void callme() {
System.out.println("a = " + a);
/ Demonstrate static variables, methods, and blocks
class Usestatic { }
static int a= 3; }
static int b;
class StaticByName {
static void meth(int x) System .out.println("x System public static void main(String[J args) {
.out.println("a System .out.println("b StaticDemo.callme();
} System.out.println("b = " +
StaticDemo.b)
{
= " + x) ; }
= " + a) ; }
= " + bl ;

static { Introducing final


System.out.println("Static block A field can be declared as final. Doing so prevents its contents from
initialized."); b = a * 4 ; being modified, making it, essentially, a constant. This means that you
must initialize a final field when it is declared. You can do this in one of
} two ways: First, you can give it a value when it is declared. Second, you
can assign it a value within a constructor.
public static void main(String[] args) { final int FILE_NEW = 1;
meth(42); final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
}
Canara Engineering College Page 27 Canara Engineering College Page 28
OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

final int FILE_SAVEAS = 4; }


final int FILE_QUIT = 5;
Subsequent parts of your program can now use FILE_OPEN, etc., as if they class InnerClassDemo {
were constants, without fear that a value has been changed. public static void main(String[J
Introducing Nested and Inner Classes args) { Outer outer= new Outer();
outer.test();
It is possible to define a class within another class; such classes are
known as nested classes. The scope of a nested class is bounded by the
}
scope of its enclosing class. Thus, if class B is defined within class A, }
then B does not exist independently of A. A nested class has access to
the members, including private members, of the class in which it is
In the program, an inner class named Inner is defined within the
nested. However, the enclosing class does not have access to the
scope of class Outer. Therefore, any code in class Inner can directly
members of the nested class. A nested class that is declared directly
access the variable outer_x. An instance method named display( ) is
within its enclosing class scope is a member of its enclosing class. It is
also possible to declare a nested class that is local to a block. defined inside Inner. This method displays outer_x on the standard
output stream. The main( ) method of InnerClassDemo creates an
There are two types of nested classes: static and non-static. A static
instance of class Outer and invokes its test( ) method. That method
nested class is one that has the static modifier applied. Because it is
creates an instance of class Inner and the display( ) method is called.
static, it must access the non-static members of its enclosing class
through an object. That is, it cannot refer to non-static members of its
enclosing class directly.
The second type of nested class is the inner class. An inner class is a
non- static nested class. It has access to all of the variables and methods
of its outer class and may refer to them dir ctly in the same way that
other non-static members of the outer class do.
/ Demonstrate an inner class.
class Outer {
int outer x = 100;

void test() {
Inner inner= new
Inner();
inner.display();

// this is an inner class


class Inner {
void display() {
System.out.println("display : outer x =" +
outer_x)

}
}

Canara Engineering College Page 29 Canara Engineering College Page 30


OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A) OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306A)

16. Explain how recursion is implemented win Java? What are the benefits of
recursion in programming?
17. Explain with a program, Java support for encapsulation through visibility
modifiers.
18. Explain the different roles of keyword ‘static’ with programs.
1. Write the general form of a class in Java. Explain class definition and member
access with suitable program. 19. Explain different roles of keyword ‘final’ with programs.

2. Explain two step procedure to create objects (class instance) with suitable 20. Explain the role of static nested classes and inner classes with programs.
program. 21. Develop a recursive version of generating, storing and display of Fibonacci series
3. Explain assignment of objects with suitable example. (Hint: use array).

4. Explain general form of method definition in Java. Explain method definitions


with suitable programs for:
a. Methods with no parameters and no return value
b. Methods with no parameters and return value
c. Methods with parameters and return value
5. Explain the role of constructors with an example.
6. Explain with a program (a) default constructor (b) parameterized constructor
7. Explain the role of ‘this’ in parameter and instance variable name collision
handling with suitable programming example.
8. Develop a Java program to define a class ‘2DBox’ with data members ‘length and
width. Define a class member method to calculate the area of 2DBox and print
box dimensions. Develop main method to illustrate the creation of objects,
member access and use of methods suitably.
9. Develop a Java program to define a class ‘Customer’ with data members like
Name, Employment and Age. Define a constructor and methods to change
employment and age. Develop main method to illustrate object creation,
member access and invocation of methods suitably.
10. Explain Java support for compile time polymorphism by taking suitable
programming example.
11. With a program, explain the effect of automatic type conversion in method
overloading.
12. Explain constructor overloading with suitable class definition and program.
13. Explain with suitable program:
a) Objects as method parameters
b) Objects as constructor parameters
14. Explain passing of primitive types and passing of objects as arguments to
methods with suitable programs.
15. Explain returning of objects from methods with a program.

Canara Engineering College Page 31 Canara Engineering College Page 32

You might also like