Session 2: Introduction to Object Oriented Programming

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Written by: Dr. JJ Shepherd
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes, Interfaces and Packages
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Topics Instance variables, set and get methods Encapsulation
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
OOP Basics Classes & Methods (c) IDMS/SQL News
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
JAVA MULTIPLE CHOICE QUESTION.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Inheritance and Polymorphism
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
Trainings 11/4 Intro to Java.
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Starting JavaProgramming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
Object Oriented Programming in java
Java Programming Language
Introduction to Object-Oriented Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 7 Objects and Classes
Presentation transcript:

Session 2: Introduction to Object Oriented Programming Java Training Session 2: Introduction to Object Oriented Programming

Objectives Classes, Objects, Methods, Instance Variables Instantiating an Object of a Class Initializing Objects with Constructors Methods with Parameters Instance Variables, set Methods and get Methods Primitive Types vs. Reference Types Static Object Vs. Class

Objects Inspired by real life. Look around you, everything is object. Example: Marker in my hand. Computer Table Chair etc…

Object (Cont.) So, What is an object? Or what is the characteristics of an object? In simplest possible term – An object is a collection of Fields/Properties/(State) and Method/(Behavior). Let’s elaborate with example.

Illustration Meto Methods Methods Fields Methods Methods

A Pen Fields Method Height Radius Color Can open it’s cap Can write Can close it’s cap

Car Fields Methods Dimensions Speed Gear Direction Number of Wheels Number of Seats Number of Doors Methods Open door Start car Accelerate Break

Car Car Object!! openDoor start accelerate break changeDirection decelerate break changeDirection Car Type Wheels Doors Speed

Object in Action So far we have understood that object has some fields and some methods. To begin coding one more thing.. Constructor!! Each java object have some special method. (we will elaborate on this in future, after understanding inheritance) One of them is constructor. It’s method that is called when a object is created( more java term is “instantiated” ). Let’s see some codes now ..

Class: GradeBook, File: GradeBook.java Instance Variables – Note the naming convention public class GradeBook { private String courseName; private String courseTeacher; public GradeBook(String name) courseName = name; } public void setCourseName(String name) public String getCourseName() { return courseName; } public void displayMessage() System.out.println("CourseName is: " + courseName); public String getCourseTeacher() return courseTeacher; Constructor Public method - Carefully note the naming convention

Class: GradeBookTest, File: GradeBookTest.java public class GradeBookTest { public static void main(Stringargs[ ]) // Instantiate or create two GradeBook objects GradeBook gb1 = new GradeBook(“OOPL”); GradeBook gb2 = new GradeBook(“DS”); System.out.printf(“gb1 course name is: %s\n”, gb1.getCourseName()); gb1.setCourseName(“DLD”); System.out.printf(“gb1 course teacher is: %s\n”, gb1.getCourseTeacher()); // The above statement is trying to print something that is not initialized yet. }

Executing the Program To compile the source files type javacGradeBook.javaGradeBookTest.java Or, javac *.java It produces two class files named GradeBook.class and GradeBookTest.class To run the program type java GradeBookTest We used GradeBookTest as the argument to java as class GradeBookTest contains the “main” method. If we use “java GradeBook” then the JVM will throw an ERROR (more dangerous than an EXCEPTION) and will terminate immediately. Exception in thread “main” java.lang.NoSuchMethodError: main

Output of “java GradeBookTest” The String courseTeacher is “null”. But using a reference which is “null” will cause unexpected results or exceptions in many cases

Some Words on Multiple Classes in the Same Directory There is a special relationship between classes that are compiled in the same directory on disk. By default, such classes are considered to be in the same package – known as the default package. Classes in the same package are implicitly imported into the source code files of other classes in the same package. Thus, an import declaration is not required when one class in a package uses another in the same package.

Primitive Types vs. Reference Types Data types in Java are divided into two categories Primitive types boolean, byte, char, short, int, long, float and double By default, all primitive-type instance variables are initialized to 0 (except boolean which is initialized to “false”). Local primitive-type variables are not initialized by default. Reference types All nonprimitive types e.g. class variables By default, all reference-type instance variables are initialized to the special value “null”. “null” is a keyword in java. Local reference-type variables are not initialized by default. Java does not permit using a non-initialized variable before assigning a value to that variable. intn; // local variable of a method, not member of any class System.out.printf(“Value if n is: %d\n”, n); // compiler error (Example provided in LocalTest.java)

Object Vs Class Class is the definition Object is the instance Example It’s just the definition No real value is attach Object is the instance An object is the entity in ram that contains data. Example We all belongs to human class, but each of us is a different human object.

Basic flow of class and object Define Class Instantiate using “new” Create object Use the Object

Static!! Static fields and methods are unique to a class. One class has one single memory allocation for a static field. Static methods are accessed without instantiating the class. Ex: System.out.println(“Hello Java”); We will elaborate it later on. Example File: StaticTest.java and StaticMainTest.java

this Reference In a class there is always a reference present name this Which points to current object. Can’t use this in a static method as static methods are not related to a object.

Core Conception of OOP Encapsulation Polymorphism Inheritance

Encapsulation The most important thing software engineers want to achieve. It is a mechanism for restricting access to some of the object's components. Provide user methods to interact but user directly can not change the fields.

Encapsulation - Example Say we have a class name Student which has a field name mark and a method name calculateGrade() We will encapsulate the algorithm related to calculating grade depending on the method. Example: StudentTest.java

Let’s try some examples Write a class of Triangle. Fields Base Height Methods Constructor, Getter and Setters calculateArea

Let’s try some examples Now take use input of base and height Then create object depending on the user input.

Let’s try points Now write a class of Point Fields Methods X Y Constructor, Getter and Setters Print() // just print the coordinates distance(Point) //calculate the distance between this point the provided point

Back to triangle Add a method name compare(Triangle) That will compare the area of this triangle and the provided triangle.

Few things JRE – the package containing only JVM and related software. Can’t compile java codes with a jre. JDK – the package containing software to run and compile java code. Java Doc – All class function’s detail is provided in java doc format. http://download.oracle.com/javase/6/docs/api/ Java Tutorial – Official java tutorial from oracle.