Presentation is loading. Please wait.

Presentation is loading. Please wait.

GC211 Data structure Lecture 3 Sara Alhajjam.

Similar presentations


Presentation on theme: "GC211 Data structure Lecture 3 Sara Alhajjam."— Presentation transcript:

1 GC211 Data structure Lecture 3 Sara Alhajjam

2 Object and class Java is an object-oriented programming language. It allows you to divide complex problems into smaller sets by creating objects. These objects share two characteristics:  state behavior

3 Object and class Let's take few examples: Lamp is an object
It can be in on or off state. You can turn on and turn off lamp (behavior). Bicycle is an object It has current gear, two wheels, number of gear etc. states. It has braking, accelerating, changing gears etc. behavior.

4 Java Class Before you create objects in Java, you need to define a class. A class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. Since, many houses can be made from the same description, we can create many objects from a class.

5 How to define a class in Java?
Here's how a class is defined in Java: class ClassName { // variables // methods }

6 Here's an example: Here, we defined a class named Lamp.
class Lamp { // instance variable private boolean isOn; // method public void turnOn() { isOn = true; } public void turnOff() { isOn = false; Here, we defined a class named Lamp. The class has one instance variable (variable defined inside class) isOn and two methods turnOn() and turnOff(). These variables and methods defined within a class are called members of the class.

7 Java Objects When class is defined, only the specification for the object is defined; no memory or storage is allocated. To access members defined within the class, you need to create objects. Let's create objects of Lamp class.

8 Java Objects This program creates two objects l1 and l2 of class Lamp.
boolean isOn; void turnOn() { isOn = true; } void turnOff() { isOn = false; class ClassObjectsExample { public static void main(String[] args) { Lamp l1 = new Lamp(); // create l1 object of Lamp class Lamp l2 = new Lamp(); // create l2 object of Lamp class

9 How to access members? How to access members?
You can access members (call methods and access instance variables) by using . operator. For example, l1.turnOn(); This statement calls turnOn() method inside Lamp class for l1 object.

10 class Lamp { boolean isOn; void turnOn() { isOn = true; } void turnOff() { isOn = false; class ClassObjectsExample { public static void main(String[] args) { Lamp l1 = new Lamp(); // create l1 object of Lamp class Lamp l2 = new Lamp(); // create l2 object of Lamp class l1.turnOn(); // access object L1 When you call the method using the above statement, all statements within the body of turnOn() method are executed. Then, the control of program jumps back to the statement following li.turnOn();

11

12 Example: Java Class and Objects
class Lamp { boolean isOn; void turnOn() { isOn = true; } void turnOff() { isOn = false; void displayLightStatus() { System.out.println("Light on? " + isOn); class ClassObjectsExample { public static void main(String[] args) { Lamp l1 = new Lamp(), l2 = new Lamp(); l1.turnOn(); l2.turnOff(); l1.displayLightStatus(); l2.displayLightStatus();

13 Output of the program When you run the program, the output will be:
Light on? true Light on? false

14 In the above program, Lamp class is created.
The class has an instance variable isOn and three methods turnOn(), turnOff() and displayLightStatus(). Two objects l1 and l2 of Lamp class are created in the main() function. Here, turnOn() method is called using l1 object: l1.turnOn(); This method sets isOn instance variable of l1 object to true. And, turnOff() method is called using l2 object: l1.turnOff(); This method sets isOff instance variable of l2 object to false. Finally, l1.displayLightStatus(); statement displays Light on? true because isOn variable holds true for l1 object. And, l2.displayLightStatus(); statement displays Light on? false because isOn variable holds false for l2 object

15 Constructor in Java Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

16 Rules for creating java constructor
There are basically two rules defined for the constructor. Constructor name must be same as its class name Constructor must have no explicit return type

17 Types of java constructors
There are two types of constructors: Default constructor (no-arg constructor) Parameterized constructor

18 Types of java constructors

19 Java Default Constructor
A constructor that have no parameter is known as default constructor. Syntax of default constructor: <class_name>(){ }

20 Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation. class Bike1{   Bike1(){System.out.println("Bike is created");}   public static void main(String args[]){   Bike1 b=new Bike1();   }   Rule: If there is no constructor in a class, compiler automatically creates a default constructor. The output: Bike is created

21 What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.

22 Example of default constructor that displays the default values
class Student3{   int id;   String name;   void display(){System.out.println(id+" "+name);}   public static void main(String args[]){   Student3 s1=new Student3();   Student3 s2=new Student3();   s1.display();   s2.display();   }   Output: 0 null In the above class, you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

23 Java parameterized constructor
A constructor that have parameters is known as parameterized constructor. Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects

24 Example of parameterized constructor
class Student4{       int id;       String name;       Student4(int i,String n){       id = i;       name = n;       }       void display(){System.out.println(id+" "+name);}       public static void main(String args[]){       Student4 s1 = new Student4(111,"Karan");       Student4 s2 = new Student4(222,"Aryan");       s1.display();       s2.display();      }   }   Output 111 Karan 222 Aryan In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.


Download ppt "GC211 Data structure Lecture 3 Sara Alhajjam."

Similar presentations


Ads by Google