Object Oriented Development, Abstraction & Inheritance

Slides:



Advertisements
Similar presentations
General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
BACS 287 Basics of Object-Oriented Programming 1.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object-oriented programming and software development Lecture 1.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Abstraction ADTs, Information Hiding and Encapsulation.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
Classes, Interfaces and Packages
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Topic: Classes and Objects
Classes C++ representation of an object
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance and Polymorphism
COMPUTER 2430 Object Oriented Programming and Data Structures I
Review: Two Programming Paradigms
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Anatomy of a Class & Method
OOP’S Concepts in C#.Net
Computer Science II Exam 1 Review.
Classes and Objects Encapsulation
Subprograms and Programmer Defined Data Type
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism and access control
Object initialization: constructors
Week 6 Object-Oriented Programming (2): Polymorphism
Encapsulation and Constructors
Object oriented vs procedural programming
More Object-Oriented Programming
COP 3330 Object-oriented Programming in C++
Introduction to Data Structure
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Review of Previous Lesson
Classes C++ representation of an object
Lecture 10 Concepts of Programming Languages
Chapter 11 Inheritance and Encapsulation and Polymorphism
Abstraction and Objects
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Object Oriented Development, Abstraction & Inheritance CSCU9A3 Data Structures, Objects and Algorithms David Cairns

Abstraction The key to building a stable system is abstraction. Abstraction allows us to form a view of a concept which considers only necessary info and hides unnecessary detail. Programming language design has seen a progression of abstraction mechanisms . . . Procedural Abstraction Data Abstraction Object Oriented Data Abstraction CSCU9A3 - Autumn 2015

Procedural Abstraction Consider a function called power which raises one number to the power of another: // Return x raised to the power p double power( double x , int p ) ; We could change the implementation (perhaps make it more efficient) and the code that invokes it won’t have to be changed. CSCU9A3 - Autumn 2015

Data Abstraction Each language provides a basic set of types e.g. float, int, char in Java and gives the user operations for manipulating variables of these types (e.g. +,-,* etc). Data Abstraction takes the idea of abstraction further - instead of just defining operations, we can define our own data types. Data Abstraction allows the definition of new types, complete with a set of operations and methods, which can then be used as if they were part of the language. We call these Abstract Data Types (ADTs). CSCU9A3 - Autumn 2015

Object-Orientation Object orientation takes data abstraction a step further via the use of: Encapsulation Inheritance Polymorphism We will look at these in more detail . . . CSCU9A3 - Autumn 2015

Encapsulation Encapsulation means that the internal details of an ADT (e.g. any data) should only be accessed via methods. Restricting access to methods stops the state of the ADT from being modified in undesirable ways. It also insulates the user of a class from dependency on internal details CSCU9A3 - Autumn 2015

Encapsulation via Access Protection Most OO languages including Java allow you to protect fields from external modification. This allows you to guarantee that objects are in a consistent state. For example, inside a Car class we’d like to be confident that the speed can never be greater than the maximum speed, i.e. we want a way to make the following illegal: Car c = new Car(); c.maxSpeed = 100.0; c.speed = 150.0; // Want to prevent this! This code violates the conceptual constraints of the class. We ideally want the compiler to enforce these constraints. To achieve this, we can specify who will be allowed to access which parts of the class. CSCU9A3 - Autumn 2015

Encapsulation public class Car { private String numberPlate; private double speed; private double maxSpeed; public Car(String numberPlate, double maxSpeed) if (maxSpeed < 0.0) throw new IllegalArgumentException(); this.numberPlate = numberPlate; this.maxSpeed = maxSpeed; } public double getMaxSpeed() { return speed; } public double getSpeed() { return maxSpeed; } CSCU9A3 - Autumn 2015

The Benefits of Encapsulation Encapsulation has three main benefits: It allows you to enforce constraints on an object’s state. It provides a simpler interface to the class. Users of the class don’t need to know everything that’s in the class in order to use it, only the public parts. It separates interface from implementation, allowing them to vary independently. For instance, we could make the numberPlate field of Car a NumberPlate class instead of a String . CSCU9A3 - Autumn 2015

Polymorphism Derives from the Greek word for “many forms”. It is central to Object-Orientation. Encourages the creation of class hierarchies, in which families of behaviour are related by inheritance. CSCU9A3 - Autumn 2015

Inheritance - Scenario Suppose that we now wish to create a traffic simulation to monitor the environmental cost of different forms of transport. In addition to cars, we might also wish to include trains, planes and boats. Using polymorphism, we can create a framework for the simulation by abstracting the key features that are common to all these forms of transport. Our framework could manage and model a set of Vehicles Car, Train, Plane and Boat are all examples of the abstract concept Vehicle. You could add your own derivation of a vehicle later You do not need to predict in advance what other types of vehicle may be used CSCU9A3 - Autumn 2015

Vehicle Class public class Vehicle { private String name; private double speed; private double maxSpeed; private double heading; public Vehicle() name = "?"; maxSpeed = 0; speed = 0; } public Vehicle(String nm) name = nm; CSCU9A3 - Autumn 2015

Vehicle Class public void turn(double degrees) { heading = (heading + degrees) % 360; } public void accelerate(double v) speed = speed + v; public double getCurrentSpeed() { return speed; } public String toString() return "Vehicle - " + name + ": " + speed; // Further methods that apply to all forms of Vehicle... CSCU9A3 - Autumn 2015

Boat Class public class Boat extends Vehicle { private double displacement; private int numSails; public Boat(String n, double displace, int sails) name = n; displacement = displace; numSails = sails; } public String toString() return "Boat - " + name + " : Displaces " + displacement; CSCU9A3 - Autumn 2015

Aircraft Class public class Aircraft extends Vehicle { private int numWings; private double wingLift; public Aircraft(String n, int wings, double lift) name = n; numWings = wings; wingLift = lift; } public String toString() return "Aircraft - " + name + " : Lift " + wingLift; CSCU9A3 - Autumn 2015

Transport Manager Class import java.util.ArrayList; public class TransportManager { private ArrayList<Vehicle> vehicles; public TransportManager() vehicles = new ArrayList<Vehicle>(); } public void addVehicle( Vehicle v) vehicles.add(v); public void describeTransport() for (Vehicle v : vehicles) System.out.println(v.toString()); CSCU9A3 - Autumn 2015

TransportTest Class CSCU9A3 - Autumn 2015 public class TransportTest { TransportManager manager = new TransportManager(); public static void main(String[] args) TransportTest test = new TransportTest(); test.go(); } public void go() Vehicle v = new Vehicle("The Vehicle"); Boat b = new Boat("Maltese Falcon",1240,15); Aircraft a = new Aircraft("Concorde",2, 5000); manager.addVehicle(v); manager.addVehicle(b); manager.addVehicle(a); manager.describeTransport(); CSCU9A3 - Autumn 2015

TransportTest – Output Vehicle - The Vehicle: Speed 0.0 Boat - Maltese Falcon : Displaces 1240.0 Aircraft - Concorde : Lift 5000.0 CSCU9A3 - Autumn 2015

Inheritance is your friend... You are already benefitting from inheritance when you build your own Java GUIs You get most of the behaviour you need for free You only add/adjust the bits you want to be different Look at API guide to see how much you are getting It's also the reason why you do not write a main method when developing your GUI code. You are inheriting from a class that already has the main method in it. CSCU9A3 - Autumn 2015