Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.

Similar presentations


Presentation on theme: "Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming."— Presentation transcript:

1 Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

2 Revision of Sessions 1-5 So far, we’ve covered: Data types Flow control (if, for, while, do-while) Software Design Software Testing Common syntax (programming grammar) Good coding style (comments, conventions, indents) These ideas apply to nearly all programming languages.

3 Session 6 - aims & objectives Feedback from evaluation forms Introduction to Object Oriented Programming Find out what Classes and Objects are Learn the difference between an Object and a Class Understand why Classes and Objects are important in object-oriented programming Learn how to create your own Classes in Java

4 Object-Oriented Programming Object-orientation is an important programming paradigm Programs are assembled into smaller elements called Objects, which represent something in the real world. Objects can contain both data and functionality. The source code responsible for defining an object as called a Class. An object is a specific instance of a class. Object-oriented code should be highly re-usable. Provides a “construction kit” approach to programming. Most OO programs will contain at least 2 classes.

5 Classes A class could be thought of as a user-defined data type, perhaps holding many pieces of information. For example, we could create a Date class which contains: day (String) date (int) month (String) year (int) An object is an instance of a class: ‘Today’ is an object of class Date. ‘Last Saturday’ is an object of class Date.

6 Classes We might want to create a class to represent a Person: The class might include... forename (String) surname (String) age, in years (int) birthday (class Date) weekday (String) day (int) month (String) year (int) One class can re-use another! “Construction kit” approach to programming!

7 Naming Convention Classes are usually named with a capital letter. Primitive variables are named with lower case letters. This makes it really easy to keep track of what is a class/object and what isn’t. Ever wondered why String has a capital S, while int, double, boolean are in lower case? String is actually a class. It is quite unusual as it can be used like a primitive variable – no other class can do this. More about Strings next week...

8 Using Classes – 1 Define your class: Classes can be defined in the same file as your application, or in their own file. For now, we will put the class definition above the application class, in the same file. class Date { // Define class variables... String weekday; int day; String month; int year; } class myprog { public static void main(String[] args) { // My application code... }

9 Using Classes – 2 Now to create an object. This is called instantiating the class. Step 1: Declare an object called ‘today’. This tells the compiler that ‘today’ is an object of class Date. Date today; Step 2: Instantiate the class using the reserved word ‘new’. This creates the object in the computer’s memory. today = new Date(); Or, you can do both steps together if you want... Date today = new Date();

10 Using Classes – 3 You can access an object’s member variables using dot syntax. So, we could set up the Today object as follows: Date today = new Date(); today.weekday = “Tuesday”; today.day = 1; today.month = “March”; today.year = 2010; We could print today’s day variable: System.out.println(today.weekday);

11 Using Classes – 4 What if we have an object inside an object? Imagine we have a class called Person, and an object of class Person called einstein. Person einstein = new Person(); Let’s set Einstein’s birthday... einstein.birthday.weekday=“Friday”; einstein.birthday.day=14; einstein.birthday.month=“March”; einstein.birthday.year=1879;

12 The purpose of OO programming Classes should model the real world – thus leading to efficient coding Classes should be reusable ‘Construction kit’ approach to programming Many useful classes have already been created. Available in libraries ready for you to use. Complex code can be created easily. We’ll talk about how to use the libraries in Session 8...

13 Coming up in Session 7... Adding functionality to classes (methods) How to get input from the user.


Download ppt "Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming."

Similar presentations


Ads by Google