Session 2 Introduction to Object Oriented Programming Objectives

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 and. • Let’s elaborate with example.

Illustration Methods Fields Meto Methods

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

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

open. Door change Directio n start Car Object!! Type Speed break Car Doors decelera te Wheels accelerate

Object in Action • So far we have understood that object has some and some. • To begin coding one more thing. . !! • 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 “ ” ). • Let’s see some codes now. .

Class: Grade. Book, File: Grade. Book. java Instance Variables – Note the naming convention public class Grade. Book { private String course. Name; private String course. Teacher; public Grade. Book(String name) { course. Name = name; } Constructor public void set. Course. Name(String name) { course. Name = name; } Public method Carefully note the naming convention public String get. Course. Name() { return course. Name; } public void display. Message() { System. out. println("Course. Name is: " + course. Name); } public String get. Course. Teacher() { return course. Teacher; } }

Class: Grade. Book. Test, File: Grade. Book. Test. java public class Grade. Book. Test { public static void main(Stringargs[ ]) { // Instantiate or create two Grade. Book objects Grade. Book gb 1 = new Grade. Book(“OOPL”); Grade. Book gb 2 = new Grade. Book(“DS”); } } System. out. printf(“gb 1 course name is: %sn”, gb 1. get. Course. Name()); gb 1. set. Course. Name(“DLD”); System. out. printf(“gb 1 course name is: %sn”, gb 1. get. Course. Name()); System. out. printf(“gb 1 course teacher is: %sn”, gb 1. get. Course. Teacher()); // The above statement is trying to print something that is not initialized yet.

Executing the Program • To compile the source files type – javac. Grade. Book. java. Grade. Book. Test. java – Or, javac *. java • It produces two class files named Grade. Book. class and Grade. Book. Test. class • To run the program type – java Grade. Book. Test • We used Grade. Book. Test as the argument to java as class Grade. Book. Test contains the “main” method. • If we use “java Grade. Book” then the JVM will throw an ERROR (more dangerous than an EXCEPTION) and will terminate immediately. – Exception in thread “main” java. lang. No. Such. Method. Error: main

Output of “java Grade. Book. Test” The String course. Teacher 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. – intn; // local variable of a method, not member of any class – System. out. printf(“Value if n is: %dn”, n); – (Example provided in Local. Test. java)

Object Vs Class • Class is the definition – 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 different . , but each of us is a

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

Static!! • Static and 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: Static. Test. java and Static. Main. Test. java

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

Core Conception of OOP Encapsulation Inheritance Polymorphism

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 which has a field name and a method name • We will the algorithm related to calculating grade depending on the method. • Example: Student. Test. java

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

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 –X –Y • Methods – Constructor, Getter and Setters – Print() // just print the coordinates – distance(Point) //calculate the distance between point the provided point

Back to triangle • Add a method name compare(Triangle) • That will compare the area of 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.
- Slides: 27