Sections 1.2-4 Classes, Objects, and Applications.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)
Introduction To Object-Oriented Programming. Object-Oriented Programming Class: Code that defines the behavior of a Java programming element called an.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 1 Getting Organized. Chapter 1: Getting Organized 1.1 – Software Engineering 1.2 – Object Orientation 1.3 – Classes, Objects, and Applications.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Chapter 1 Getting Organized. Waterfall Life-Cycle Model.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 10 Thinking in Objects
Sections Inheritance and Abstract Classes
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 9 Thinking in Objects
Interface.
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Chapter 9 Thinking in Objects
CIS 199 Final Review.
Chapter 1 Getting Organized
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Sections Classes, Objects, and Applications

1.2 Object Orientation Objects represent –information: we say the objects have attributes –behavior: we say the objects have responsibilities Objects can represent “real-world” entities such as bank accounts Objects are self-contained and therefore easy to implement, modify, and test for correctness Object-oriented classes, when designed properly, are very easy to reuse

1.3 Classes, Objects, and Applications An object is an instantiation of a class Alternately, a class defines the structure of its objects. A class definition includes variables (data) and methods (actions) that determine the behavior of an object. Example: the Date class (next slide)

public class Date { protected int year; protected int month; protected int day; public static final int MINYEAR = 1583; // Constructor public Date(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } // Observers public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public int lilian() { // Returns the Lilian Day Number // of this date. // Algorithm goes here. } public String toString() // Returns this date as a String. { return(month + "/" + day + "/" + year); }

Java Access Control Modifiers Within the Class Within Subclasses in the Same Package Within Subclasses in Other Packages Everywhere publicXXXX protectedXXX packageXX privateX

Class Diagram for Date Class

Objects Date myDate = new Date(6, 24, 1951); Date yourDate = new Date(10, 11, 1953); Date ourDate = new Date(6, 15, 1985);

Applications An object-oriented application is a set of objects working together, by sending each other messages, to solve a problem. In object-oriented programming a key step is identifying classes that can be used to help solve a problem. An example – using our Date class to solve the problem of calculating the number of days between two dates (next 3 slides)

DaysBetween Design display instructions prompt for and read in info about the first date create the date1 object prompt for and read in info about the second date create the date2 object if dates entered are too early print an error message else use the date.lilian method to obtain the Lilian Day Numbers compute and print the number of days between the dates

// // DaysBetween.java by Dale/Joyce/Weems Chapter 1 // // Asks the user to enter two "modern" dates and then reports // the number of days between the two dates. // import java.util.Scanner; public class DaysBetween { public static void main(String[] args) { Scanner conIn = new Scanner(System.in); int day, month, year; System.out.println("Enter two 'modern' dates: month day year"); System.out.println("For example January 12, 1954 would be: "); System.out.println(); System.out.println("Modern dates occur after " + Date.MINYEAR + "."); System.out.println(); System.out.println("Enter the first date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); Date date1 = new Date(month, day, year);

System.out.println("Enter the second date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); Date date2 = new Date(month, day, year); if ((date1.getYear() <= Date.MINYEAR) || (date2.getYear() <= Date.MINYEAR)) System.out.println("You entered a 'pre-modern' date."); else { System.out.println("The number of days between"); System.out.print(date1); System.out.print(" and "); System.out.print(date2); System.out.print(" is "); System.out.println(Math.abs(date1.lilian() - date2.lilian())); }

1.4 Organizing Classes During object-oriented development hundreds of classes can be generated or reused to help build a system. The task of keeping track of these classes would be impossible without organizational structure. Two of the most important ways of organizing Java classes are –inheritance: classes are organized in an “is-a” hierarchy –packages: let us group related classes together into a single named unit

Inheritance Allows programmers to create a new class that is a specialization of an existing class. We say that the new class is a subclass of the existing class, which in turn is the superclass of the new class.

Example of Inheritance public class IncDate extends Date { public IncDate(int newMonth, int newDay, int newYear) { super(newMonth, newDay, newYear); } public void increment() // Increments this IncDate to represent the next day. // For example if this = 6/30/2005 then this becomes 7/1/2005. { // increment algorithm goes here }

Declaring and Using Date and IncDate Objects Date myDate = new Date(6, 24, 1951); IncDate aDate = new IncDate(1, 11, 2001); System.out.println("mydate day is: " + myDate.getDay()); System.out.println("aDate day is: " + aDate.getDay()); aDate.increment(); System.out.println("the day after is: " + aDate.getDay()); See Extended Class Diagram next slide.

Packages Java lets us group related classes together into a unit called a package. Packages provide several advantages. They –let us organize our files. –can be compiled separately and imported into our programs. –make it easier for programs to use common class files. –help us avoid naming conflicts (two classes can have the same name if they are in different packages).

Using Packages A Java compilation unit can consist of a file with –the keyword package followed by an identifier indicating the name of the package: package someName; –import declarations, to make the contents of other packages available: import java.util.Scanner; –one or more declarations of classes; exactly one of these classes must be public The classes defined in the file are members of the package. The imported classes are not members of the package. The name of the file containing the compilation unit must match the name of the public class within the unit.

Using Packages Each Java compilation unit is stored in its own file. The Java system identifies the file using a combination of the package name and the name of the public class in the compilation unit. Java restricts us to having a single public class in a file so that it can use file names to locate all public classes. Thus, a package with multiple public classes must be implemented with multiple compilation units, each in a separate file.

Using Packages In order to access the contents of a package from within a program, you must import it into your program: import packagename.*; import packagename.Classname; The Java package rules are defined to work seamlessly with hierarchical file systems: import ch03.stacks.*;