OOP1 Unit-4
OOP1 Unit-4
3140705
Unit 4:
Objects and
Classes
Looping
What we will learn
Unit-4
Introduction
Large Data Handling
Class
Class is derived datatype, it combines members of different datatypes
into one.
Defines new datatype (primitive ones are not enough).
For Example : Car, College, Bus etc..
This new datatype can be used to create objects.
A class is a template for an object .
Example :
class Car{
String company;
String model;
double price;
double milage;
………
}
Car Class
Class:
Car
Properties (Describe)
Company
Model Methods
Color (Functions)
Mfg. Year Start
Price Drive
Fuel Type Park
Mileage On_break
Gear Type On_lock
Power Steering On_turn
Anti-Lock braking
system
Object
An object is an instance of a class.
An object has a state and behavior.
Example: A dog has
states - color, name, breed as well as
behaviors – barking, eating.
The state of an object is stored in fields (variables), while methods
(functions) display the object's behavior.
What is an Object?
Philosophy of Object Oriented
Our real world is nothing but classification of objects
E.g. Human, Vehicle, Library, River, Watch, Fan, etc.
Real world is organization of different objects which have their own
characteristics, behavior
Characteristic of Human: Gender, Age, Height, Weight, Complexion,
etc.
Behavior of Human: Walk, Eat, Work, React, etc.
Characteristic of Library: Books, Members, etc.
Behavior of Library: New Member, Issue Book, Return Book etc.
The OO philosophy suggests that the things manipulated by the program
should correspond to things in the real world.
Classification is called a Class in OOP
Real world entity is called an Object in OOP
Characteristic is called Property in OOP
Behavior is called Method in OOP
What is an Object?
What is an Object?
Pe Board Lapto
n p
Result Bank
Account
Logical objects…
What is an Object?
An Object is a key to
understand Object
Oriented Technology.
An entity that has state
and behavior is known
as an object. e.g.,
Mobile, Car, Door, OBJECT
Laptop etc
Each and every object
posses
Identity
State
Behavior
Object: A Real-World Entity
CA
R
Clas
s
OBJECT:
CAR
Events
Properties
(Describe) On_Start
Manufacturer On_Parked
Model On_Brake
Color
Methods
Year
(Actions)
Price
Start
Drive
Park
Object: A Real-World Entity
Object: A Real-World Entity
Objects of Class Bird
Classes and Objects
Classes and Objects
Cube Width:20cm
Object 2
• Name Depth:20cm Cube : C2
• Height Cube : C2 Name: C2
• Width Height:5cm
• Depth
Width:5cm
Cube : C3 Depth:5cm
Objec
t 3 : C3
Cube
Name: C3
Height:10cm
Width:10cm
Depth:10cm
Class and Objects
Construct
or
Outp
ut
balance=0.0
No-Argument
Constructor
No-Argument Constructor: MyMain.java
1. class Cube { 13.class MyMain{
2. double width; 14.public static void
main(String[] args){
3. double height;
15. Cube c=new Cube(
4. double depth;
5. Cube()
16. }
6. {
17.}
7. System.out.println("
If you implement any constructor then you no
Constructing cube"); longer receive a default constructor from Java
compiler.
8. width = 10;
9. height = 10;
10. depth = 10;
11. }//Cube()
12.}//class
Parameterized
Constructor
Parameterized Constructor: MyMain.java
1. class Cube { 10.class MyMain{
2. double width, height, depth; 11.public static void
main(String[] arg
3. Cube(double w, double h, double
{
d)
12. Cube c=new
4. { System.out.println("
Cube(10,10,10);
Constructing cube"); 13. }
5. width = w; 14.}
6. height = h;
7. depth = d;
8. }//Cube()
9. }//class
Parameterized Constructor: MyMain.java with
method
1. class Cube { 13.class MyMain{
2. double width,height,depth; 14.public static void
main(String[] args){
3. Cube(double w, double h, double d)
4. { System.out.println("Constructing 15. Cube c=new Cube(10,10,10);
cube");
16. c.calVolume();
5. width = w;
17. }
6. height = h;
18.}
7. depth = d;
8. }//cube()
9. void calVolume(){
10. System.out.println("Volume="
+width*height*depth);
11. } //calVolume()
12.} //class
Parameterized Constructor: method with return
value
1. class Cube { 13.class MyMain{
2. double width,height,depth; 14.public static void
main(String[] args){
3. Cube(double w, double h, double d)
15. double vol;
4. { System.out.println("Constructing
cube"); 16.Cube c=new Cube(10,10,10);
5. width = w; 17. vol=c.calVolume();
6. height = h; 18. System.out.println("
7. depth = d; Volume="+vol);
8. }//cube() 19. }
9. double calVolume(){ 20.}
Outp
10. return width*height*depth; ut
11. }//calVolume() Constructing
12.}//class cube
Volume=1000.0
this
‘this’ keyword
class Box {
double length;
double breadth;
double height;
Box(doublel,
Box(double length,
doubledouble breadth,
b, double h) { double
height) {
System.out.println("Constructing Box");
this.length
length
length =
= l; = length;
length; Creates
length ambiguity
is instance for
variable
this.breadth = breadth;
breadth = breadth; compiler
as well as length is formal
breadth = b;
this.height = height;
height = height;
height = h; parameter of method
}
void volume() {
double volume = length * breadth * height;
System.out.println("Volume is " + volume);
}
}
class BoxDemo {
public static void main(String args[]) {
Box myBox1 = new Box(10,20,30);
Box myBox2 = new Box(3,6,9);
myBox1.volume();
myBox2.volume();
}
‘this’ Keyword
this is a reference variable that refers to the
current object.
this can be used to invoke current object's
method. This
this() can be used to invoke current class
constructor
this can be passed as a parameter to
constructor and method call.
this can be used to return the current object
from the method.
Copy Constructor
Copy Constructor
It is a special type of constructor that
is used to create a new object using Constructor 2(Constructor
Constructor 1()
1)
the existing object of a class that had {
{
been created previously. …
…
}
It creates a new object by initializing }
the object with the instance of the Copy Constructor
same class.
Copy Constructor: MyProgramCopy.java
1. class Student{ 19. class MyProgramCopy {
2. String name; 20. public static void main(String[]
3. int rollno; args){
4. Student(String s_name, int s_roll){ 21. float area;
5. 22. Student s1=new
System.out.println("ConstructorInvoked"); Student("darshan",101);
6. this.name=s_name; 23. //invoking Copy Constructor
7. this.rollno=s_roll; 24. Student s2=new Student(s1);
8. } //Constructor1 25. s1.display();
26. s2.display();
9. Student(Student s){ 27. } //main()
//CopyConstructor 28. } //class myProgram
10. System.out.println("CopyConstructor
Outp
ut
Invoked"); Constructor Invoked
11. this.name=s.name; CopyConstructor Invoked
12. this.rollno=s.rollno; name=darshan
13. } //Constructor2 rollno=101
name=darshan
14. public void display(){ rollno=101
15. System.out.print("name="+name);
16. System.out.println("
Advantages of Copy Constructor
It is easier to use when our class contains a
complex object with various parameters. Constructor 2(Constructor
Constructor 1()
1)
Whenever we need to add all the field of a {
{
…
class to another object, then just send the …
}
reference of previously created object. }
Static in Java
Nested Class
B: Nested
Class
Package (Not part of this Unit)
A Package can be defined as a grouping of related types providing
access protection and name space management.
Programmers can define their own packages to bundle group of
classes/interfaces, etc.
Packages are used in Java in order to
1. prevent naming conflicts
2. control access,
3. make searching/locating of classes/interfaces easier.
It is a good practice to group related classes implemented so that a
programmer can easily determine that the classes, interfaces are related.
Creating a package
To create a package you need to write package statement followed by
the name of the package.
Syntax : package package_name;
Example : package darshan_student;
class Student {
// code
}
The package statement should be the first line in the source file.
There can be only one package statement in each source file, and it
applies to all types in the file.
If a package statement is not used then the class/interfaces will be put
into an unnamed package.
Package (Example)
package myPackage;
public class Animal {
public String name;
public void eat(){
System.out.println("Organic Food !!!!");
}
public static void main(String[] args){
Animal a = new Animal();
a.eat();
}
}
.
Represent the current
To compile directory
javac –d . Animal.java
To Run the class file
java myPackage.Animal
import keyword
import keyword is used to import built-in and user-defined packages into
your java source file so that your class can refer to a class that is in
another package by directly using its name.
There are 3 different ways to refer to class/interface that is present in
different package
import the class/interface you want to use.
import all the classes/interfaces from the package.
Using fully qualified name.
We can import
importajava.util.Scanner;
class/interface of other package using a import keyword
at the firstpublic
line ofclass
code.DemoImport {
Example : public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
// Code
}
}
import (importing all class/interface)
We can import all the classes/interfaces of other package using a import
keyword at the first line of code with the wildcard (*).
Example :import java.util.*;
public class DemoImport {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Date d = new Date();
// Code
}
}
It is possible to use classes from other packages without importing the
class using fully qualified name of the class.
Example :java.util.Scanner s = new java.util.Scanner(System.in);
Static Import
The static import feature of Java 5 facilitate the java programmer to
access any static member of a class directly.
Advantage : Less coding is required if you have to access any static
member of a class more frequently.
Disadvantage : If you overuse the static import feature, it makes the
program unreadable and unmaintainable.