GC211 Data structure Lecture 3 Sara Alhajjam.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Road Map Introduction to object oriented programming. Classes
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java Programming static keyword.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 1 Dr Walid M. Aly 1 Lecture 4 Object- Oriented Programming (CS243) Group home page:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Classes, Interfaces and Packages
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Methods.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Constructors and Destructors
Topic: Classes and Objects
The need for Programming Languages
Concepts of Object Oriented Programming
Static data members Constructors and Destructors
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
GC101 Introduction to computer and program
Yanal Alahmad Java Workshop Yanal Alahmad
Road Map Introduction to object oriented programming. Classes
CompSci 230 Software Construction
CS 302 Week 11 Jim Williams, PhD.
Java LESSON 7 Objects, Part 1
Unit-2 Objects and Classes
CSC 113 Tutorial QUIZ I.
Contents Introduction to Constructor Characteristics of Constructor
Chapter 8 Objects and Classes Part 1
C++ Classes and Objects
Object Oriented Programming in java
Constructors and Destructors
S.VIGNESH Assistant Professor/CSE, SECE
S.VIGNESH Assistant Professor, SECE
Classes Lecture 7 from Chapter /1/11.
JAVA Constructors.
Constructors under inheritance Variable Shadowing
Basics of OOP A class is the blueprint of an object.
Session 2: Introduction to Object Oriented Programming
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 7 Objects and Classes
Previous Lecture: Today’s Lecture: Reading (JV):
CMSC 202 Constructors Version 9/10.
Presentation transcript:

GC211 Data structure Lecture 3 Sara Alhajjam

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

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.

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.

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

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.

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.

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

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.

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();

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();

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

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

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.

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

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

Types of java constructors

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

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

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

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.

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

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.