0% found this document useful (0 votes)
35 views10 pages

Oop L4

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
35 views10 pages

Oop L4

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
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
You are on page 1/ 10

Object Oriented

Programming

BITS Pilani
CS F213
Dubai Campus SAPNA SADHWANI
Contents
• Classes and Object Examples
• Object Reference
• Constructor
• Example
• Method overloading
• Example
• Constructor overloading
• Example
• this keyword
• Example
Source:
1. Chapter 1, Cay Horstmann, Object Oriented Design & Patterns, John Wiley & Sons, 2006, 2nd Edition
2. Chapter 6, 7, Herbert Schildt, The complete Reference Java 2, 5th Edition, Tata McGraw Hill.
3. https://www.programiz.com/java-programming/constructors
4. https://beginnersbook.com/2013/05/method-overloading/

BITS Pilani, Dubai Campus


Classes and Objects
• Example 1: Example 1.java

BITS Pilani, Dubai Campus


Object Reference
class Student {
String name; int age;
public Student ( String aName, int aAge) {
name = aName; age = aAge;
}
public void setAge( int aAge ) { age = aAge; }
public void printAge() {System.out.println
(age); }
}
public class StudentTester1 {
public static void main (String[] args) {
Student s1 = new Student("abc", 10);
Student s2 = s1;
s2.setAge(20); // set s2’s age
s1.printAge(); // print s1’s age : what is o/p
s2.printAge(); // print s2’s age : what is o/p
}
} • Example 2: StudentTester1.java
BITS Pilani, Dubai Campus
Object Reference
• What is memory organization when an object is
declared and instantiated.
Student s1 = new Student (“abc”, 10);
Student
s1 name = “abc”
age = 10

Student s2 = s1;
s2 also is a reference to the same object s1.
Its not a copy.
Student
s1 name = “abc”
age = 10
s2

BITS Pilani, Dubai Campus


Object Reference
s2.setAge(20); s1 Student
name = “abc”
s2 age = 20

s1.printAge(); it prints 20.

Solution is to create a new object.


public class StudentTester {
public static void main (String[] args) {
Student s1 = new Student("abc", 10);
Student s2 = new Student(“def”, 15);
s2.setAge(20); // set s2’s age
s1.display(); // print s1’s age : what is o/p
}

BITS Pilani, Dubai Campus


Constructor
• A class contains constructors that are invoked to create
objects from the class blueprint.
• Constructor declarations look like method declaration.
• They use the name of the class.
• Do not have return type.
• A constructor is called automatically when a new
instance of an object is created.
• Types of constructors
• Non-argument constructor
• Parameterised constructor
• Non-argument constructor: Example3.java
• Parameterised constructor: Example4.java

BITS Pilani, Dubai Campus


Method Overloading
• Method Overloading is a feature that allows a class to
have more than one method having the same name, if
their argument lists are different.
• Three ways of method overloading
• Number of parameters Example5.java
add(int, int)
add(int, int, int)
• Data type of parameters
Example6.java
add(int, int)
add(int, float)
• Sequence of Data type of parameters Example7.java
add(int, float) Invalid:
add(float, int) int add(int, int)
float add(int, int)
BITS Pilani, Dubai Campus
Constructor Overloading
• Sometimes there is a need of initializing an object
in different ways. This can be done using
constructor overloading.
• Example8.java

BITS Pilani, Dubai Campus


BITS Pilani
Dubai Campus

Thank You!

You might also like