This document contains lecture notes on object-oriented programming concepts in Java including classes, methods, method overloading, parameter passing, recursion, storage for Java programs, access protection, static members, nested classes, and object references. Examples are provided to illustrate key concepts such as recursive methods, call by value vs. call by reference parameter passing, and how objects are stored in memory.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views29 pages
More On Classes - OOP-5
This document contains lecture notes on object-oriented programming concepts in Java including classes, methods, method overloading, parameter passing, recursion, storage for Java programs, access protection, static members, nested classes, and object references. Examples are provided to illustrate key concepts such as recursive methods, call by value vs. call by reference parameter passing, and how objects are stored in memory.
Method s General form of a method: type methodname1(parameter-list) { body } Overloading methods in Java - In Java it is possible to define two or more methods in a class with same name. - In such case the methods are said to be overloaded. - And this process is referred to as method overloading. - This is how Java implements polymorphism- “one interface multiple methods”
Passing Objects as parameters class Line { int length; Line (int l) {length=l; } boolean compare(L ine x) { if(this.leng th==x.leng th) return Object Oriented Programming BITS Pilani, Hyderabad Campus class Module62 { public static void main(String args[]) { Line l1=new Line(10); Line l2=new Line(10); boolean b=l1.compare(l2); if (b) System.out.printl n(" Both l1 and l2 are of same length"); else } System.out.printl n(" l1 and l2 are of different length"); Object Oriented Programming BITS Pilani, Hyderabad Campus Parameter passing (Call-b-value/call-by-reference) class Line { int length; Line(int l) { length=l;} void doubleVal( int a) { a=a*2;} void doubleLength(Line l) { l.length=l.length*2;} }
class Module63 { public static void main(String args[]) { Line l1=new Line(2); System.out.println("Call by value :"); System.out.println("value of Line length before doubled is: "+ l1.length); l1.doubleVal(l1.length); System.out.println("value of Line length after doubled is: "+ l1.length); System.out.println("Now Call by reference :"); System.out.println("length of the line before doubled is: "+ l1.length); l1.doubleLength(l1); System.out.println("l ength of the line after doubled is: "+ l1.length); } Object Oriented Programming } BITS Pilani, Hyderabad Campus Recursio n Java supports recursion. –Recursion is the process of defining something in terms of itself. –A method can call itself. Such methods are called recursive methods.
Storage for Java Programs The memory used by a running java program is organized into two areas, called segments: the stack segment and the heap segment.
The stack is where memory is allocated for local variables
within methods.
The heap segment provides more stable storage of data for a
program; memory allocated in the heap remains in existence for the duration of a program. Therefore static variables and objects are allocated on the heap.
When method is called space for parameters and variables is allocated on the stack. The structure of the stack includes a stack frame for each active method/procedure. There may be several frames in the stack at once for a given method/procedure if it is recursive.
When a method calls itself, new local variables and parameters are allocated storage on the stack, and the method code is executed with these new variables from the start. As each recursive call returns, the old local variables and parameters are removed from the stack, and the execution resumes at the point of call inside the method. Execution of recursive calls are slow because of overhead due to function calls.
Many recursive calls to a method can cause in stack overflow. In such case Java run-time will throw an error. But this is a rare event. The main advantage of recursion is that they can be used to write clearer code for algorithms that are iterative in nature. While writing iterative methods we must include an IF statement somewhere to force the method to return without recursive call being executed. Otherwise it will never return.
Object Oriented Programming BITS Pilani, Hyderabad Campus Static members The static can be attached before instance variable or a method name while declaring the same. static variables - they are global to class - all instances will share the same copy - not per instance basis - can be called with class name without instances static methods - can call only static methods - can access only other static data - can not refer to this or super - can be called with class name without instances Object Oriented Programming BITS Pilani, Hyderabad Campus Nested Class A Nested class is a class defined in another class. If class B is defined inside class A, it is known to A but not outside A. That is B does not exist independent of A. All members (including private) of A are accessible by B. But, A can not access members of B. It is also possible to declare a nested class local to a block. Any code outside Outer class cannot create an inner class object.
InnerDemo { public static void main(String args[]) { Outer out=new Outer(); out.test(); } } Classes created Outer.class Outer$Inner.class Object Oriented Programming BITS Pilani, Hyderabad Campus class Outer { C:\Users\Admin\JavaPrograms>jav ac int outer_x; InnerClassDemo1.java Outer() {outer_x=10;} InnerClassDemo1.java:19: error: class Inner cannot find symbol { int m; Inner in =new Inner(55); Inner(int a) {m=a;} ^ } symbol: class Inner } location: class InnerClassDemo1 class InnerClassDemo1 { public static void main(String args[]) { Inner in =new Inner(55); System.out.println(" value of Inner clas m is: "+ in.m); } }
class Demo { public static void main(String args[]) { Box b1=new Box(10); Box b2=b1; System.out.println(" box b1 lenth is :"+b1.length); System.out.println(" box b2 lenth is :"+b2.length); Box b3=new Box(10); System.out.println(" box b3 lenth is :"+b3.length); if(b1==b2) System.out.println(" box b1 and b2 are same:"); else System.out.println(" box b1 and b2 are not same:"); if(b1==b3) System.out.println(" box b1 and b3 are same:"); else System.out.println(" box b1 and b3 are not same:"); } }