Download object oriented programming with Java and more Study notes Object Oriented Programming in PDF only on Docsity!
CST 205- OBJECT ORIENTED
PROGRAMMING USING JAVA
MODULE- 2
PART-
Module 2 Core Java Fundamentals
Primitive Data types - Integers, Floating Point Types, Characters, Boolean. Literals, Type Conversion and
Casting, Variables, Arrays, Strings, Vector class.
Operators - Arithmetic Operators, Bitwise Operators, Relational Operators, Boolean Logical Operators,
Assignment Operator, Conditional (Ternary) Operator, Operator Precedence.
Control Statements - Selection Statements, Iteration Statements and Jump Statements.
Object Oriented Programming in Java - Class Fundamentals, Declaring Objects, Object Reference,
Introduction to Methods, Constructors, this Keyword, Method Overloading, Using Objects as Parameters,
Returning Objects, Recursion, Access Control, Static Members, Final Variables, Inner Classes, Command Line
Arguments, Variable Length Arguments.
Inheritance - Super Class, Sub Class, The Keyword super, protected Members, Calling Order of Constructors,
Method Overriding, the Object class, Abstract Classes and Methods, using final with Inheritance
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
// return true if o is equal to the invoking
object
boolean equals(Test o) {
if(o.a == a && o.b == b)
return true;
else return false;
class PassOb { public static void main(String args[])
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " +
ob1.equals(ob2));
System.out.println("ob1 == ob3: " +
ob1.equals(ob3));
OUTPUT
ob1 == ob2: true
ob1 == ob3: false
// Constructor public ObjectParameter(int attribute1, String attribute2, double attribute3) { this.attribute1 = attribute1; this.attribute2 = attribute2; this.attribute3 = attribute3; } //Passing object2 as parameter public void myMethod(ObjectParameter obj) { obj.setmethod(5); System.out.println("Attribute 1: " + obj.attribute1); System.out.println("Attribute 2: " + obj.attribute2); System.out.println("Attribute 3: " + obj.attribute3); } public void myMethod() { System.out.println("Attribute 1: " + attribute1); System.out.println("Attribute 2: " + attribute2); System.out.println("Attribute 3: " + attribute3); } } public class ObjectParameter{ public static void main(String[] args) { ObjectParameter myObject1 = new ObjectParameter(10, "Hello", 3.14); ObjectParameter myObject2 = new ObjectParameter(20, "World", 6.28); System.out.println("Object2 before passing through method: "); myObject2.myMethod(); System.out.println("Object2 after passing through method: "); myObject1.myMethod(myObject2); // myObject2.myMethod(); //to check whether call by value or call by reference } private int attribute1; private String attribute2; private double attribute3; public void setmethod(int x) { attribute1= x; }
Object2 before passing through method: Attribute 1: 20 Attribute 2: World Attribute 3: 6. Object2 after passing through method: Attribute 1: 5 Attribute 2: World Attribute 3: 6.
- When you pass a primitive type to a method, it is passed by value. Thus, a copy of the argument is made, and what occurs to the parameter that receives the argument has no effect outside the method.
- When you pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference.
- Keep in mind that when you create a variable of a class type, you are only creating a reference to an object.
- Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument.
- This effectively means that objects act as if they are passed to methods by use of call-by-reference.
- Changes to the object inside the method do affect the object used as an argument.
# IMPORTANT
Returning Objects
- A method can return any type of data, Primitive data (int ,float, char, double etc.),class types(objects) 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 OUTPUT
- As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference to it is returned to the calling routine.
- The preceding program makes another important point: Since all objects are dynamically allocated using new, you don’t need to worry about an object going out of scope because the method in which it was created terminates.
- The object will continue to exist as long as there is a reference to it somewhere in your program.
- When there are no references to it, the object will be reclaimed the next time garbage collection takes place.
Passing arguments to function
● Java always passes parameter variables by value.
● Object variables in Java always point to the real object in the memory heap.
● A mutable object's value can be changed when it is passed to a method.
● An immutable object's value cannot be changed, even if it is passed a new value.
● “Passing by value” refers to passing a copy of the value.
● “Passing by reference” refers to passing the real reference of the variable in memory.
Effect:
•// Primitive types(int,char,double etc.) are passed by value.
•// Mutable Objects are passed by reference.
•// Immutable Objects are passed by value. Eg String
Why copy constructor is required? Sometimes, we face a problem where we required to create an exact copy of an existing object of the class. There is also a condition, if we have made any changes in the copy it should not reflect in the original one and vice-versa. For such cases, Java provides the concept of a copy constructor. Copy Constructor In Java, a copy constructor is a special type of constructor that creates an object using another object of the same Java class. It returns a duplicate copy of an existing object of the class. Use of Copy Constructor We can use the copy constructor if we want to:
- Create a copy of an object that has multiple fields.
- Generate a deep copy of the heavy objects.
- Avoid the use of the Object.clone() method. Advantages of Copy Constructor
- If a field declared as final, the copy constructor can change it.
- There is no need for typecasting.
- Its use is easier if an object has several fields.
- Addition of field to the class is easy because of it. We need to change only in the copy constructor.
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 allows a method to call itself.
- A method that calls itself is said to be recursive.
- The classic example of recursion is the computation of the factorial of a number.
- The factorial of a number N is the product of all the whole numbers between 1 and N.
- For example, 3 factorial is 1 × 2 × 3 ×, or 6. Here is how a factorial can be computed by use of a recursive method:
OUTPUT
Recursion