+ All Categories
Home > Education > Object Oriented Programming_Lecture 3

Object Oriented Programming_Lecture 3

Date post: 22-Jan-2017
Category:
Upload: mahmoud-alfarra
View: 356 times
Download: 0 times
Share this document with a friend
31
Object Oriented Programming Chapter 1: Introduction to OOP Prepared by: Mahmoud Rafeek Alfarra 2016
Transcript

Object Oriented Programming

Chapter 1: Introduction to OOP

Prepared by: Mahmoud Rafeek Alfarra

2016

Outlines◉What is Object-Oriented Programming ?

◉Procedural vs. Object-Oriented Programming

◉OO Programming Concepts

◉Concept of Objects and classes

◉UML Class Diagram

◉Visibility Modifiers and Accessor Methods

◉Full Example

من عمل صالحا فلنفسه ومن أساء )كم ترجعون ( فعليها ثم إلى رب

Lecture Let’s think on fieldes of class

3

Sample classclass Pencil {

public String color = “red”;public int length;public float diameter;

public static long nextID = 0;

public void setColor (String newColor) { color = newColor;

}}

o Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes.

Access control modifiers

A member has package or default accessibility when no accessibility modifier is specified.

A standard design strategy is to make all fields private and provide public getter methods for them.

o private: private members are accessible only in the class itself.

o package: package members are accessible in classes in the same package and the class itself.

o protected: protected members are accessible in classes in the same package, in subclasses of the class, and in the class itself.

o public: public members are accessible anywhere the class is accessible.

Access control modifiers

Access control modifiers

public class Pencil {public String color = “red”;public int length;public float diameter;private float price;public static long nextID = 0;public void setPrice (float

newPrice) { price = newPrice;} }

public class CreatePencil {public static void main (String args[]){

Pencil p1 = new Pencil();p1.price = 0.5f;

} }

Pencil.java

CreatePencil.java

o final once initialized, the value cannot be changed. often be used to define named constants. static final fields must be initialized when the class is

initialized. non-static final fields must be initialized when an object

of the class is constructed.

More Access control modifiers

Methods

OverloadingConstructorParameter Valuesmain method

o main methodo The system locates and runs the main method for a

class when you run a programo Other methods get execution when called by the main

method explicitly or implicitly.o Must be public, static and void.

main method

o A class can have more than one method with the same name as long as they have different parameter list.

public class Pencil {. . .public void setPrice (float newPrice) {

price = newPrice;}public void setPrice (Pencil p) {

price = p.getPrice(); }

}

Overloading

o Parameters are always passed by value. public void method1 (int a) { a = 6;}

public void method2 ( ) { int b = 3; method1(b); // now b = ?

// b = 3}

Parameter Values

o When the parameter is an object reference, it is the object reference, not the object itself, getting passed.

Parameter Values

public void method1 (Car a) { …}

class PassRef{public static void main(String[] args) { Pencil plainPencil = new Pencil("PLAIN"); System.out.println("original color: " + plainPencil.color);

paintRed(plainPencil);

System.out.println("new color: " + plainPencil.color);}

public static void paintRed(Pencil p) { p.color = "RED"; p = null;}

}

Parameter is an object reference

color: PLAIN

color: PLAIN

color: RED

color: RED NULL

p

plainPencil

plainPencil

plainPencil p

plainPencil p

o If you change any field of the object which the parameter refers to, the object is changed for every variable which holds a reference to this object.

o You can change which object a parameter refers to inside a method without affecting the original reference which is passed.

o What is passed is the object reference, and it’s passed in the manner of “PASSING BY VALUE”!

Parameter is an object reference

o Constructors are a special kind of methods that are invoked to construct objects.

Constructors

Circle() {}

Circle(double newRadius) { radius = newRadius;}

o A constructor with no parameters is referred to as a no-arg constructor.

o Constructors must have the same name as the class itself. o Constructors do not have a return type—not even void. o Constructors are invoked using the new operator when an

object is created. o Constructors play the role of initializing objects.

Constructors

o A class may be defined without constructors. In this case, a no-arg constructor with an empty body is implicitly defined in the class.

o This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class.

Constructors

Copying Variables of Object Types

i

Primitive type assignment i = j Before:

1

j

2

i

After: 2

j

2

c1

Object type assignment c1 = c2 Before:

c2

c1

After:

c2

c1: Circle radius = 5

C2: Circle radius = 9

c1: Circle radius = 5

C2: Circle radius = 9

o As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2.

o The object previously referenced by c1 is no longer referenced.

o This object is known as garbage. o Garbage is automatically collected by JVM.

Garbage Collection

The JVM will automatically collect the space of null object.

Variables

Static Variablesclass variables ConstantsInstance Variables =

Instance Vs Static

o Statico Variables are shared by all the

instances of the class.o public static int x;o Methods are not tied to a specific

object. o public static void sum (int a, int

b) {… }

o Instanceo Variables belong to a

specific instance.o public int x;o Methods are invoked by an

instance of the class.o public void sum (int a, int b) {… }

Instance Vs Static

o Static constants are final variables shared by all the instances of the class.

Static constants

o The get and set methods are used to read and modify private properties.

o The get and set methods are used to read and modify private properties.

Accessor/Mutator Methods

public void getName( ){return name; // as example}

public void setName(String name){This.name = name; // as example}

Static constants

o An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a).

Static constants

PracticesGroup 1Develop 2 Accessor Methods.

Group 2Give 3 examples to use final modefier.

Group 3Diffrenciate between Access modefiers.

Group 4Give 3 examples to overloaded methods.

Group 5By drawing, Compare between Copying Variables primitives data types and Object Types.

Group 6Develop a simple class.

THANKS!

Any questions?You can find me at:

Fb/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra


Recommended