e0-245: aspdipanjan/e0_245/e0245-asp... · 2015-01-30 · 2 dipanjan gope •intent is asynchronous...

26
E0-245: ASP Lecture 5: OOPs Classes and Objects Dipanjan Gope

Upload: others

Post on 10-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

E0-245: ASP

Lecture 5: OOPs Classes and Objects

Dipanjan Gope

Page 2: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

2

Dipanjan Gope

• Intent is asynchronous

• Sending notification a service?

• SensorEventListener component

• Why does BroadcastReciever not extend service?

• Can 2 activities use same screen space?

• Lifecycle: what is the limit to #of processes?

Questions from Lecture 3

Page 3: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

3

Dipanjan Gope

Android Stack

Ref: http://www.tutorialspoint.com/android/android_architecture.htm

Page 4: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

4

Dipanjan Gope

JAVA and C++ mainly: (others C# and Objective-C)

• Object oriented programing: Classes and objects

• Inheritance, polymorphism, abstract class, const

• Templates and generics

• Data structures

• Standard library, JCF, STL

• Complexity analysis

• Multithreading and synchronization

• Good programming styles

Module 1: OOPs and DS

Page 5: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

5

Dipanjan Gope

• Assembler

• Compiler

• Interpreter

• Just-in-time Compiler

Code Conversion

Page 6: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

6

Dipanjan Gope

JAVA VM

http://www.javatpoint.com/features-of-java

Page 7: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

7

Dipanjan Gope

JAVA VM

http://www.javatpoint.com/features-of-java

Page 8: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

8

Dipanjan Gope

• First Programming Language

- Fortran

- 1957

- IBM

• First OOPs Language

- Simula

- 1967

- Norwegian Computing Center

Fun Facts

Page 9: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

9

Dipanjan Gope

• Java

- 1994

- James Gosling, Sun Microsystems

- Licensed by Netscape for navigator

• C language

- 1972

- Dennis Ritchie

- Bell Labs

Fun Facts

Page 10: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

10

Dipanjan Gope

Evolution of Programming Languages

Paul Boutin and Bret Hailpern at IBM Research and Todd Proebsting at Microsoft, The Retrocomputing Museum, and Gio Wiederhold at Stanford University.

Page 11: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

11

Dipanjan Gope

Top 10 Programming Languages

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

Page 12: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

12

Dipanjan Gope

int a; # how many bytes?

int *pA; # how many bytes?

int &rA = a; # how many bytes?

Recap: Pointers and References

Page 13: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

13

Dipanjan Gope

When to use pointers?

- Pointer arithmetic

- NULL initialization

Recap: Pointers vs References

Pointers References

NULL Yes No

Point changed to a different object

Yes No

Initialization Anytime On creation

Page 14: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

14

Dipanjan Gope

• Class

• Object

• Polymorphism

• Inheritance

• Abstraction

• Encapsulation

• Overloading

Object Oriented Programming System

Page 15: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

15

Dipanjan Gope

• Code Reuse and Recycling

• Encapsulation: Hide details, prevent tampering

• Design benefits

• Software maintenance

• Run-time errors become compiler errors

Advantages of OOPs

https://www.cs.drexel.edu/~introcs/Fa12/notes/06.1_OOP/Advantages.html?CurrentSlide=3

Page 16: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

16

Dipanjan Gope

class Rectangle

{

# Methods

Public:

Private:

Protected:

# Field: Member data

Public:

Private:

Protected:

}

Class

Fields: Member data Local Variables: Variables in piece of code Parameters: Variables in function declaration

Page 17: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

17

Dipanjan Gope

Public, Protected, Private

JAVA Modifiers C++ Access Specifier

http://docs.oracle.com/javase/tutorial/ java/javaOO/accesscontrol.html

Specifier Class Project Sub-class

public Y Y Y

protected Y N Y

no specifier Same as private

private Y N N

Page 18: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

18

Dipanjan Gope

• Instance of a class

• Just like: int i;

• Occupies memory in RAM;

• E.g. Rectangle rectangle1;

Object

Page 19: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

19

Dipanjan Gope

class Student

{

private:

float CGPA;

int rollNumber;

double height;

int schoolID; // string schoolName;

}

Object: Example

Page 20: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

20

Dipanjan Gope

• Static member data:

- common to all objects of the class

- single memory location for all classes

- e.g. static string schoolName

• Static member function:

- belongs to class rather than object

- may be invoked without object instance

- can access static variables

Static

Page 21: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

21

Dipanjan Gope

• Special member function

• Same name as the class

• No return type (not even void)

• Used for:

- variable initialization

- memory allocation (perhaps)

- called on “new” or instantiation

Constructor

Page 22: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

22

Dipanjan Gope

• Constructor overloading

- Multiple forms of constructors (polymorph)

- different input arguments

• Special types of constructors:

- Default constructor (different for diff compilers)

- Copy constructor

Constructor

Page 23: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

23

Dipanjan Gope

Constructor

Rectangle rectangle1; Rectangle* pRectangle2 = new Rectangle; Rectangle rectangle1(1,1.5); Rectangle* pRectangle2 = new Rectangle(1,1.5);

Rectangle rectangle1 = new Rectangle; Rectangle rectangle2 = new Rectangle(1,1.5);

JAVA reference-based

C++ pointer-based

Page 24: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

24

Dipanjan Gope

• Special member function

• Same name as the class with ~

• No return type (not even void)

• Used for:

- memory creation

- called on “out of scope” or “delete”

• JAVA: Garbage collector

Destructor (C++)

Page 25: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

25

Dipanjan Gope

Project Organization

JAVA C++

Page 26: E0-245: ASPdipanjan/E0_245/E0245-ASP... · 2015-01-30 · 2 Dipanjan Gope •Intent is asynchronous •Sending notification a service? •SensorEventListener component •Why does

26

Dipanjan Gope

Code Organization

JAVA C++

Rectangle Class

Main