Object Oriented Programming Budditha Hettige Department of Computer Science
Overview Introduction to OOP Class and Objects Constructors Encapsulation Composition Inheritance Polymorphism Interfaces Packages Exception handling File Handling GUI Programming OOP Software development 7/11/2015 Budditha Hettige (budditha@yahoo.com) 2
Introduction (OOP) OOP is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods 3
Advantages of OOP Provides a clear modular structure for programs OOP makes it easy to maintain and modify existing code (new objects can be created with small differences to existing ones) OOP provides a good framework for code libraries Software components can be easily adapted and modified by the programmer. 4
Modular Structure School 5
Exercise Identify suitable state and behaviors (Attributes and methods) for the following Student Bank Account Book Employee Sales Summary Course Result 7/11/2015 Budditha Hettige (budditha@yahoo.com) 6
Concepts of OOP Objects Classes Encapsulation Inheritance Polymorphism Data Abstraction 7
What is an Objects? Object is a software bundle of related state and behavior Characteristics: state and behavior 8
What is a class? A Java class is a group of Java methods and variables Object is an instance of a class Example class Person { // Methods // Variables } 9
Example Create a Java Class Account with suitable methods and variables 7/11/2015 Budditha Hettige (budditha@yahoo.com) 10
Class Modifiers Public Abstract Final Default (none) 11
Class Modifiers public: anyone can create an object of the defined class. only one public class per file, must have same name as the file (this is how Java finds it!). default: is non-public (if you don't specify "public"). Java Programming: OOP 12
Class Modifiers abstract: modifier means that the class can be used as a superclass only. no objects of this class can be created. final: if its definition is complete and no subclasses are desired or required a final class never has any subclasses, the methods of a final class are never overridden Java Programming: OOP 13
Field Modifiers public protected private none or package or default Static Final 14
Field Modifiers public: any method (in any class) can access the field. protected: any method in the same package can access the field, or any derived class. private: only methods in the class can access the field. Default: is that only methods in the same package can access the field. Java Programming: OOP 15
Field Modifiers Static: Fields declared static are called class fields (class variables). others are called instance fields. There is only one copy of a static field, no matter how many objects are created. Final:class and instance variables (static and nonstatic fields) may be declared final. The keyword final means: once the value is set, it can never be changed static final int BUFSIZE=100; final double PI=3.14159; Java Programming: OOP 16
Method Modifiers public protected none or package or default private final abstract static native synchronized 17
Method Modifiers private/protected/public: same idea as with fields. abstract: no implementation given, must be supplied by subclass. the class itself must also be declared abstract Java Programming: OOP 18
Method Modifiers static: the method is a class method, it doesn't depend on any instance fields or methods, and can be called without first creating an object. final: the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass). Java Programming: OOP 19
Method Modifiers native: the method is written in some local code (C/C++) - the implementation is not provided in Java. synchronized: only one thread at a time can call the method. Java Programming: OOP 20
Method Overloading You can overload methods: same method name, different parameters. you can't just change return type, the parameters need to be different. Method overloading is resolved at compile time. int CounterValue() { } return counter; Won't Work! double CounterValue() { return (double) counter; } Java Programming: OOP 21
All Possible Combinations of Features and Modifiers Modifier Class Variable Method public yes yes yes protected no yes yes None (default) yes yes yes private no yes yes final yes yes yes abstract yes no yes static no yes yes native no no yes transient no yes no volatile no yes no synchronized no no yes strictfp yes no yes 22
Example Create a class Student with following methods print(); input() Create a class Length with following methods Print() input() Add(Lenth1, Length2) 23
Example contd. Create a class Name with 3 attribute (First name, middle name and last name) and include following methods Print(); input() Create a class name Date with 3 attribute (Day, Month and year) and include following methods print() Input() printformat1() // 23.5.2015 PrintFormat2() // 23 rd May 2015 24