17
Introduction to Object Oriented Programming Java

Introduction to Object Oriented Programming Java

Embed Size (px)

Citation preview

Page 1: Introduction to Object Oriented Programming Java

Introduction to Object Oriented Programming

Java

Page 2: Introduction to Object Oriented Programming Java

Resources

If you are new to programming in Java, there is an online book which is serves as a good introduction:

●Thinking in Java 3rd edition

This can be found at:

http://www.mindview.net/Books/TIJ/

Page 3: Introduction to Object Oriented Programming Java

Development Environment

You can use the command line and notepad to edit and compile java programs.

You can also use jedit BlueJ is a tool which will help you format

code, compile and test your programs.

Page 4: Introduction to Object Oriented Programming Java

OOP Introduction

Introduced in the 60s as a proposed

solution to the sprawl of software Allowed designers to describe the

system entities in plain language and design functions and attributes for each entity.

The interaction or interfaces between the entities can also be designed easily.

Page 5: Introduction to Object Oriented Programming Java

An OO Approach

class Student{/* attributes */String name;int age;int courseNumber;int fees;boolean enrolled;

/* Methods */public getName(){ ... }public int getAge() {...}public int getFees() { ...}}

class StudentRecords{/* attributes */Student [] database;Course [] allCourses;

/* Methods */public voidenrollStudent(Student s){ ... }public void disenrollStudent(Student s) { ... }public int getTotalStudents(){ ... }}

Page 6: Introduction to Object Oriented Programming Java

Advantages of OO Programming

The software is modular and reusable

i.e. we could reuse our code for Student in an examRecords system

It is extendable, and maintainable i.e. if we decided to store students

mobile phone numbers, it'd just be a small change to the Student class.

Page 7: Introduction to Object Oriented Programming Java

OO Programming Vocabulary

● class

● object

● method/attribute

● inheritance

● encapsulation

● abstraction

● polymorphism

Page 8: Introduction to Object Oriented Programming Java

Class

A class is a definition of a well defined entity, containing attributes and methods. This is a real world entity

class Employees {

}

Page 9: Introduction to Object Oriented Programming Java

Object

An object is an instance of a class. e.g. Employee e1 = new Employee(); e1 is now an object of type Employee.

Page 10: Introduction to Object Oriented Programming Java

Method

A method is simply a well defined function within a class.

Example methods in Employee... getSalary(); getDOB(); add(); remove();

Page 11: Introduction to Object Oriented Programming Java

Attribute

● An attribute is an instance variablewhich represents some data within a class. For

example, all employees have a name and a salary.

so...● class Student{String name;float salary;..etc.}

Page 12: Introduction to Object Oriented Programming Java

Inheritance

● Inheritance allows you to define classes

which inherit the behaviour and attributes of

other classes.

● For example, you may have one Employee

class, but 3 different types of employees

(permanent staff, part time staff and managers).

Rather than write 3 entirely separate classes

you can inherit the standard employee

characteristics from a base class.

Page 13: Introduction to Object Oriented Programming Java

Inheritance Example

Employee

Permanent PartTime Manager

forthnightSalary weeklySalary monthlySalary

Page 14: Introduction to Object Oriented Programming Java

Advantages of this approach

Less code, therefore easier to maintain Reusable Employee class

Page 15: Introduction to Object Oriented Programming Java

Encapsulation

● We've seen how to specify the

behaviour of an object, the idea of

encapsulation is to hide the details of

how something is achieved.

● e.g. If you are using a class that

someone else has written, you want to

be able to call a method (Save File for

example) without knowing what is

going on in the background.

Page 16: Introduction to Object Oriented Programming Java

Abstraction

● Abstraction a principle used to enable

inheritance.

● Using abstraction you develop general

classes that are appropriate to the

problem, and then for specific problems

inherit from these general classes.

● e.g. an Employee class models the typical

behaviours of employees, but for parttime staff

you'll need more specific methods.

Page 17: Introduction to Object Oriented Programming Java

Polymorphism

● Polymorphism is a type of encapsulation that is aimed at decoupling software systems.

● Polymorphism literally means “manyforms“, and its idea is to allow subclasses

to have their own specific implementations of methods.

e.g. calculateSalary is a different processfor our three types of employee.