Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Similar documents
Software Design and Analysis for Engineers

Programming overview

Imperative Languages!

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Introduction to Inheritance

Inheritance: Definition

Java Object Oriented Design. CSC207 Fall 2014

Inheritance, and Polymorphism.

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Encapsulation. Mason Vail Boise State University Computer Science

JAVA MOCK TEST JAVA MOCK TEST II

Object Oriented Technology

Chapter 6 Introduction to Defining Classes

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

CMSC 132: Object-Oriented Programming II

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CSC Inheritance. Fall 2009

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Object-Oriented Programming Concepts

QUESTIONS FOR AVERAGE BLOOMERS

Principles of Software Construction: Objects, Design, and Concurrency

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

G Programming Languages - Fall 2012

15CS45 : OBJECT ORIENTED CONCEPTS

Handout 9 OO Inheritance.

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

MechEng SE3 Lecture 7 Domain Modelling

CS 617 Object Oriented Systems Lecture 5 Classes, Classless World:Prototypes, Instance Variables, Class Variables, This/Self 3:30-5:00 pm Thu, Jan 17

ECE 122. Engineering Problem Solving with Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance and Polymorphism

Chapter 4 Java Language Fundamentals

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

7. C++ Class and Object

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

Inheritance and Subclasses

1B1b Classes in Java Part I

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Programming in C# Inheritance and Polymorphism

Chapter 10 Defining Classes

1 Shyam sir JAVA Notes

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Chapter 2: Java OOP I

Arrays Classes & Methods, Inheritance

CLASSES AND OBJECTS IN JAVA

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance and Encapsulation. Amit Gupta

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

CS5233 Components Models and Engineering

Implementing Classes (P1 2006/2007)

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Java Programming. Manuel Oriol, March 22nd, 2007

C++ Important Questions with Answers

Implementing Classes

Object Oriented Software Development CIS Today: Object Oriented Analysis

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

OBJECT ORİENTATİON ENCAPSULATİON

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

ITI Introduction to Computing II

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Inheritance. Transitivity

What are the characteristics of Object Oriented programming language?

Data Structures (list, dictionary, tuples, sets, strings)

Introduction to Eiffel

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

More on Inheritance. Interfaces & Abstract Classes

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Principles of Object Oriented Programming. Lecture 4

Inheritance -- Introduction

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

D Programming Language

OVERRIDING. 7/11/2015 Budditha Hettige 82

COP 3330 Final Exam Review

Chapter 4 Defining Classes I

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

ITI Introduction to Computing II

Practice problem on defining and using Class types. Part 4.

Data Abstraction. Hwansoo Han

The Final Exam Paper. Duration: 2 hours Reading: 15 minutes Total marks: 65 Hurdle: 32.5

More On inheritance. What you can do in subclass regarding methods:

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java Application Development

Assertions. Assertions - Example

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object Oriented Programming. Solved MCQs - Part 2

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Transcription:

Object-oriented basics Object Class vs object Inheritance Overloading Interface 1

The object concept Object Encapsulation abstraction Entity with state and behaviour state -> variables behaviour -> methods ( functions in C++) Provides Modularity Information hiding Both very important for large-scale programming 2

Example :BankAccount public class BankAccount { private int amount = 0; BankAccount(){ } BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int } BankAccount(int init){ amount = init; } public synchronized void deposit(int value){ amount += value; } public synchronized void withdraw(int value){ amount -= value; } public int getbalance(){ return amount; } 3

Class The class A type or specification used to design objects An object is a specific instance of a class Example BankAccount specifies the account type BankAccount a, b, c, d; Defines particular instances when created 4

Class declaration 5

Object life cycle Create AClass myobj = new AClass(); new operator and constructor Constructor initialises the object Use Manipulate state and call methods myobj.method(); By default, all variables are private Removal Automatic garbage collector Ref:myObj o (AClass) 6

Visibility Classes can control what they show to the outside world Public - open to all Protected - open to derived (child) classes Private - only for current class Package - open for other classes in package 7

References Java uses references BankAccount a, b; a = new BankAccount(1000); b = a; a.withdraw(100); Make sure you understand this 8

Class relationships Programs consists of (many) objects They must have some connection in order to work Three main relations has aggregation is inheritance knows association 9

Has relation Whole-part relationship One way relationship - account: BankAccount AccountHolder Thread -account BankAccount - amount: int = 0 - chan: Channel + AccountHolder(name :String) + AccountHolder(account :BankAccount, name :String) + run() : void ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int 10

Is relation Inheritance General-specific relation Common state and behaviour can be stored in super (parent) class More specific types created with subclasses (child or derived class Overloading Subclass method can have new implementation BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int Sav ingsaccount - interest: double + getinterest() : double 11

Use of inheritance Structuring Incremental development BankAccount b = new BankAccount(10); SavingsAccount s = new SavingsAccount(100); BankAccount bb; SavingsAccount ss; bb = s; // valid, s IS a BankAccount // but no getinterest() ss = b; // invalid, b is NOT a SavingsAccount 12

Knows relation Equal relation Either one- or two-way Implemented as a reference Passed in constructor or method call BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int -chan Channel - buffer: Object - empty: boolean ~ Channel() + send(data :Object) : void + receive() : Object 13

Package Concept to group related classes Helps in Finding classes Avoiding name conflicts Controlling access (visibility) Implementation Directory hierarchy E.g.: a/b/c/bankaccount.java a.b.c.bankaccount.java Use Requires fully qualified name Declare you package (package a.b.c;) Import classes from other packages (import q.b.*;) 14

Interface Simple but perhaps most difficult concept to understand An interface is a contract Named collection of methods an object must implement Specification of behaviour NO implementation Large-scale programming Separates what from how Applications can change implementation without affecting other parts BUT! Interfaces cannot change! 15

How to use Declare as interface Account { } class BankAccount implements Account {..} Program for interfaces, not classes AccountHolder has Account! Account acc = new BankAccount(); BankAccount can be replaced by other classes As long as they implement Account What about SavingsAccount? 16 «interface» Account + transfer() : void + withdraw() : void + getbalance() : void + deposit() : void «realize» BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int