SlideShare a Scribd company logo
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
2
7/11/2015 Budditha Hettige (budditha@yahoo.com)
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
5
School
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
Java Programming: OOP 12
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 13
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
Field Modifiers
• public
• protected
• private
• none or package or default
• Static
• Final
14
Java Programming: OOP 15
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 16
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 non-
static 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;
Method Modifiers
• public
• protected
• none or package or default
• private
• final
• abstract
• static
• native
• synchronized
17
Java Programming: OOP 18
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 19
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 20
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 21
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;
}
double CounterValue() {
return (double) counter;
}
Won't Work!
All Possible Combinations of Features
and Modifiers
22
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
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() // 23rd May 2015
24
Ad

More Related Content

Similar to 01-class-and-objects java code in the .pdf (20)

core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
YashikaDave
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
baabtra.com - No. 1 supplier of quality freshers
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
Amr Abd El Latief
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Week 7 Java Programming Methods For I.T students.pdf
Week 7 Java Programming Methods For I.T students.pdfWeek 7 Java Programming Methods For I.T students.pdf
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
Sherihan Anver
 
Quick Interview Preparation for C# All Concepts
Quick Interview Preparation for C# All ConceptsQuick Interview Preparation for C# All Concepts
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
Chap02
Chap02Chap02
Chap02
Jotham Gadot
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in Lotuscript
Bill Buchan
 
Introduction to oops
Introduction to oopsIntroduction to oops
Introduction to oops
Umamaheshwariv1
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8
John Godoi
 
12_oop templa.pptx
12_oop templa.pptx12_oop templa.pptx
12_oop templa.pptx
RokaKaram
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
Unit II Inheritance ,Interface and Packages.pptx
Unit II   Inheritance ,Interface and Packages.pptxUnit II   Inheritance ,Interface and Packages.pptx
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Week 7 Java Programming Methods For I.T students.pdf
Week 7 Java Programming Methods For I.T students.pdfWeek 7 Java Programming Methods For I.T students.pdf
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
Sherihan Anver
 
Quick Interview Preparation for C# All Concepts
Quick Interview Preparation for C# All ConceptsQuick Interview Preparation for C# All Concepts
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in Lotuscript
Bill Buchan
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8
John Godoi
 
12_oop templa.pptx
12_oop templa.pptx12_oop templa.pptx
12_oop templa.pptx
RokaKaram
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
Unit II Inheritance ,Interface and Packages.pptx
Unit II   Inheritance ,Interface and Packages.pptxUnit II   Inheritance ,Interface and Packages.pptx
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 

More from sithumMarasighe (10)

2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf
sithumMarasighe
 
project management cpm and peat digram .pdf
project management   cpm and peat digram .pdfproject management   cpm and peat digram .pdf
project management cpm and peat digram .pdf
sithumMarasighe
 
02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf
sithumMarasighe
 
oop_full java coding and esay to understand.pdf
oop_full java coding  and  esay to  understand.pdfoop_full java coding  and  esay to  understand.pdf
oop_full java coding and esay to understand.pdf
sithumMarasighe
 
03-inheretance java code programming .pdf
03-inheretance java code programming .pdf03-inheretance java code programming .pdf
03-inheretance java code programming .pdf
sithumMarasighe
 
04_-note voerrding java code simapl-oop.pdf
04_-note  voerrding java code  simapl-oop.pdf04_-note  voerrding java code  simapl-oop.pdf
04_-note voerrding java code simapl-oop.pdf
sithumMarasighe
 
AI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdfAI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdf
sithumMarasighe
 
Tree Guide March 2021 .pdf
Tree Guide March 2021               .pdfTree Guide March 2021               .pdf
Tree Guide March 2021 .pdf
sithumMarasighe
 
CSS & PHP10101010110101010101010100.pdf
CSS  & PHP10101010110101010101010100.pdfCSS  & PHP10101010110101010101010100.pdf
CSS & PHP10101010110101010101010100.pdf
sithumMarasighe
 
Apple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptxApple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptx
sithumMarasighe
 
2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf
sithumMarasighe
 
project management cpm and peat digram .pdf
project management   cpm and peat digram .pdfproject management   cpm and peat digram .pdf
project management cpm and peat digram .pdf
sithumMarasighe
 
02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf
sithumMarasighe
 
oop_full java coding and esay to understand.pdf
oop_full java coding  and  esay to  understand.pdfoop_full java coding  and  esay to  understand.pdf
oop_full java coding and esay to understand.pdf
sithumMarasighe
 
03-inheretance java code programming .pdf
03-inheretance java code programming .pdf03-inheretance java code programming .pdf
03-inheretance java code programming .pdf
sithumMarasighe
 
04_-note voerrding java code simapl-oop.pdf
04_-note  voerrding java code  simapl-oop.pdf04_-note  voerrding java code  simapl-oop.pdf
04_-note voerrding java code simapl-oop.pdf
sithumMarasighe
 
AI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdfAI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdf
sithumMarasighe
 
Tree Guide March 2021 .pdf
Tree Guide March 2021               .pdfTree Guide March 2021               .pdf
Tree Guide March 2021 .pdf
sithumMarasighe
 
CSS & PHP10101010110101010101010100.pdf
CSS  & PHP10101010110101010101010100.pdfCSS  & PHP10101010110101010101010100.pdf
CSS & PHP10101010110101010101010100.pdf
sithumMarasighe
 
Apple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptxApple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptx
sithumMarasighe
 
Ad

Recently uploaded (20)

Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-02 notes [BCG402-CG&V].pdf
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-02 notes [BCG402-CG&V].pdfCOMPUTER GRAPHICS AND VISUALIZATION :MODULE-02 notes [BCG402-CG&V].pdf
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-02 notes [BCG402-CG&V].pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptxMODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Data Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptxData Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Data Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptxData Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Ad

01-class-and-objects java code in the .pdf

  • 1. Object Oriented Programming Budditha Hettige Department of Computer Science
  • 2. Overview • Introduction to OOP • Class and Objects • Constructors • Encapsulation • Composition • Inheritance • Polymorphism • Interfaces • Packages • Exception handling • File Handling • GUI Programming • OOP Software development 2 7/11/2015 Budditha Hettige ([email protected])
  • 3. 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
  • 4. 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
  • 6. 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 ([email protected]) 6
  • 7. Concepts of OOP • Objects • Classes • Encapsulation • Inheritance • Polymorphism • Data Abstraction 7
  • 8. What is an Objects? • Object is a software bundle of related state and behavior • Characteristics: – state and behavior 8
  • 9. 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
  • 10. Example • Create a Java Class Account with suitable methods and variables 7/11/2015 Budditha Hettige ([email protected]) 10
  • 11. Class Modifiers • Public • Abstract • Final • Default (none) 11
  • 12. Java Programming: OOP 12 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").
  • 13. Java Programming: OOP 13 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
  • 14. Field Modifiers • public • protected • private • none or package or default • Static • Final 14
  • 15. Java Programming: OOP 15 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.
  • 16. Java Programming: OOP 16 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 non- static 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;
  • 17. Method Modifiers • public • protected • none or package or default • private • final • abstract • static • native • synchronized 17
  • 18. Java Programming: OOP 18 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
  • 19. Java Programming: OOP 19 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).
  • 20. Java Programming: OOP 20 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.
  • 21. Java Programming: OOP 21 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; } double CounterValue() { return (double) counter; } Won't Work!
  • 22. All Possible Combinations of Features and Modifiers 22 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
  • 23. Example • Create a class “Student” with following methods – print(); – input() • Create a class “Length” with following methods – Print() – input() – Add(Lenth1, Length2) 23
  • 24. 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() // 23rd May 2015 24