SlideShare a Scribd company logo
1 of 33
Java 102: Intro to Object-oriented
Programming with Java
Java 102
• Object-oriented Programming
– Fundamentals
• Classes & Objects
• Methods & Constructors
• Encapsulation
• Inheritance
– Libraries & Clients
Java 102: Intro to Object-
oriented Programming with Java
Fundamentals
Classes
• Java is an object-oriented language
• Its constructs represent concepts from the
real world.
• Each Java program has at least one class that
knows how to do certain actions or has
properties
• Classes in Java may have methods and
properties (a.k.a. attributes or fields)
Example 1: Car Class
Objects
• Objects are created using Classes as a
blueprint
• Each object is an instance of a Class
• Objects must be instantiated before they can
be accessed or used in a program
• Objects are instantiated using the new
keyword
Example 2: Creating Car Objects
• These two Car instances are created with the
new operator:
Car car1 = new Car();
Car car2 = new Car();
• Now the variables car1 and car2 represent
new instances of Car:
car1.color=“blue”;
car2.color=“red”;!
Encapsulation
public class Car {
private String make;
private String model;
private String color;
public void setColor(String color){
this.color = color;
}
public String getColor(){
return this.color;
}
void startEngine(){
System.out.println("starting
engine");
}
void stopEngine(){
System.out.println("stopping
engine");
}
variables store the state of the objects that
are created from this class. Ideally. these
should not be accessed directly by other
objects
getters and setters encapsulation the state
of objects. All access to object variables
should be done through this mechanism
Methods encapsulate the behavior of
objects created from this class
Packages
• Packages are used for grouping classes within
a java project
• Packages provide a way to organise code into
cohesive groups
• They are just folders on the filesystem
• IDE’s replace the slashes with a dot as part of
convention
Hands-on Exercise
Creating Objects
Functions
• A sequence of program instructions that perform
a specific task that are packaged as a unit
• Takes zero or more input arguments.
• Returns one output value.
• May have side effects
• Examples
– Scientists use mathematical functions to calculate
formulas.
– Programmers use functions to build modular
programs.
Methods
• Two types…
– Class Level Methods
– Object/instance level methods
• Class level methods are referred to as static methods
• Method declaration referred to as the signature
– Method name
– Parameter types
• Examples.
– Built-in static methods: Math.random(), Math.abs(),
Integer.parseInt().
– I/O libraries: System.out.print(),
– User-defined methods: main(), etc
Benefits of Methods?
• Methods enable you to build a new layer of
abstraction.
– Takes you beyond pre-packaged libraries.
– You build the functionality you need
• Process.
– Step 1: identify a useful feature
– Step 2: implement it
– Step 3: use it (re-use it in any of your programs).
Anatomy of a Java Method
Scope
• The block of code that can refer to that named variable
• E.g. A variable's scope is code following in the block.
• Best practice: declare variables to limit their scope
Hands-on Exercise
Working with Methods
Constructors
• Constructors are special methods
• They are called only once when the class is being instantiated:
Tax t = new Tax(40000, “CA”,4);
• They must have the same name as the class.
• They can’t return a value and you don’t use void as a return type.
public class Tax {
// class variables / fields
private double grossIncome;
private String state;
private int dependents;
// Constructor
public Tax (double grossIncome, String state, int depen){
// class variable initialization
this.grossIncome = grossIncome;
this.state = state;
this.dependents = dependents;
}
}
Method Overloading
• Method overloading means having a class with more
than one method having the same name but
different argument lists.
class LoanShark{
int calcLoanPayment(int amount, int numberOfMonths){
// by default, calculate for New York state
calcLoanPayment(amount, numberOfMonths, “NY”);
}
int calcLoanPayment(int amount, int numberOfMonths, String state){
// Your code for calculating loan payments goes here
}
}
Hands-on Exercise
Method Overloading
Inheritance
• Ability to define a new class based on an existing one.
• E.g. lets create a James Bond Car from our existing Car class
Class Inheritance
<<abstract>>
Person
Employee Contractor
extends
Method Overriding
• If a subclass has the method with the same name
and argument list, it will override (suppress) the
corresponding method of its ancestor.
• Method overriding comes handy in the following
situations:
• The source code of the super class is not
available, but you still need to change its
functionality
• The original version of the method is still valid in
some cases, and you want to keep it as is
Hands-on Exercise
Inheritance
Classes and Objects Summary
• A class declaration names the class and encloses the class body between
braces
• The class name can be preceded by modifiers e.g. public, private
• The class body contains fields, methods, and constructors
• A class uses fields to contain state information and uses methods to
implement behaviour
• Constructors that initialize a new instance of a class share its name and
look like methods without a return type
• Specify a class variable or a class method by using the static keyword in
the member's declaration
• Class variables are shared by all instances of a class and can be accessed
through the class name as well as an instance reference
• You create an object from a class by using the new operator and a
constructor
• The garbage collector automatically cleans up unused objects
Java 102: Intro to Object-oriented
Programming with Java
Java Libraries
Definitions
• Library - a module whose methods are
primarily intended for use by many other
programs.
• Client - a program that calls a library.
• API - the contract between client and
implementation.
• Implementation - a program that implements
the methods in an API.
Example: Standard Random
• A library to generate pseudo-random numbers.
Source: http://search.dilbert.com/comic/Tour%20Of%20Accounting
Standard Random API
Standard Random Implementation
public class StdRandom {
//between 0 and N-1
public static int uniform (int N){
return (int)(Math.random() * N);
}
// between lo and hi
public static double uniform (double lo, double hi){
return lo + (int)(Math.random() * (hi-lo));
}
// truth with probability
public static boolean bernoulli(double p){
return Math.random() < p;
}
}
Hands-on Exercise
Using a Java Library
Java Libraries Summary
• Why use libraries?
– Makes code easier to understand.
– Makes code easier to debug.
– Makes code easier to maintain and improve.
– Makes code easier to reuse.
Homework Exercise
• Invent and program any sample application to
illustrate inheritance.
• For example, think of the classes Cat and Dog,
Man and Woman, or a store inventory that
has to be discounted...
Further Reading
• OO Concepts - http://docs.oracle.com/javase/tutorial/java/concepts/index.html
• Classes and Objects – http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
• Interfaces and Inheritance - http://docs.oracle.com/javase/tutorial/java/IandI/index.html
• Tools, tips and tricks for Unit Testing Java applications - http://www.junit.org

More Related Content

What's hot

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Sagar Verma
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_javaHoang Nguyen
 
Core java lessons
Core java lessonsCore java lessons
Core java lessonsvivek shah
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
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
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 

What's hot (14)

Core java
Core javaCore java
Core java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core java
Core javaCore java
Core java
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Core java lessons
Core java lessonsCore java lessons
Core java lessons
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
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...
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Scala basic
Scala basicScala basic
Scala basic
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 

Viewers also liked

Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agileagorolabs
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overviewagorolabs
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
How to Initiate Kids Into Java Programming
How to Initiate Kids Into Java ProgrammingHow to Initiate Kids Into Java Programming
How to Initiate Kids Into Java ProgrammingFelix Roberge
 
Introduction to Java Programming Language
Introduction to Java Programming Language Introduction to Java Programming Language
Introduction to Java Programming Language Karwan Mustafa Kareem
 
Drupal Camp Kiev 2012 - High Performance Drupal Web Sites
Drupal Camp Kiev 2012 - High Performance Drupal Web SitesDrupal Camp Kiev 2012 - High Performance Drupal Web Sites
Drupal Camp Kiev 2012 - High Performance Drupal Web SitesJean-Baptiste Guerraz
 
CON 3431 - Introducing Java Programming to Kids
CON 3431 - Introducing Java Programming to KidsCON 3431 - Introducing Java Programming to Kids
CON 3431 - Introducing Java Programming to KidsArun Gupta
 
Csc1401 lecture07 -external memory
Csc1401   lecture07 -external memoryCsc1401   lecture07 -external memory
Csc1401 lecture07 -external memoryIIUM
 
It Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaIt Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaKevlin Henney
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5Bianca Teşilă
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)Waheed Khalid
 
Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmerspnr15z
 

Viewers also liked (20)

Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agile
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overview
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
How to Initiate Kids Into Java Programming
How to Initiate Kids Into Java ProgrammingHow to Initiate Kids Into Java Programming
How to Initiate Kids Into Java Programming
 
Java kid8x11
Java kid8x11Java kid8x11
Java kid8x11
 
Introduction to Java Programming Language
Introduction to Java Programming Language Introduction to Java Programming Language
Introduction to Java Programming Language
 
Java threading
Java threadingJava threading
Java threading
 
Drupal Camp Kiev 2012 - High Performance Drupal Web Sites
Drupal Camp Kiev 2012 - High Performance Drupal Web SitesDrupal Camp Kiev 2012 - High Performance Drupal Web Sites
Drupal Camp Kiev 2012 - High Performance Drupal Web Sites
 
Java basic
Java basicJava basic
Java basic
 
CON 3431 - Introducing Java Programming to Kids
CON 3431 - Introducing Java Programming to KidsCON 3431 - Introducing Java Programming to Kids
CON 3431 - Introducing Java Programming to Kids
 
Edp111c z2-001
Edp111c z2-001Edp111c z2-001
Edp111c z2-001
 
Csc1401 lecture07 -external memory
Csc1401   lecture07 -external memoryCsc1401   lecture07 -external memory
Csc1401 lecture07 -external memory
 
It Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaIt Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in Java
 
Enum Report
Enum ReportEnum Report
Enum Report
 
Java Day-2
Java Day-2Java Day-2
Java Day-2
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmer
 

Similar to Java 102 intro to object-oriented programming in java

Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#Muhammad Younis
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructorsJan Niño Acierto
 

Similar to Java 102 intro to object-oriented programming in java (20)

Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
Java
JavaJava
Java
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Advanced oops concept using asp
Advanced oops concept using aspAdvanced oops concept using asp
Advanced oops concept using asp
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Java2
Java2Java2
Java2
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Java
JavaJava
Java
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Recently uploaded

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 

Recently uploaded (20)

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 

Java 102 intro to object-oriented programming in java

  • 1. Java 102: Intro to Object-oriented Programming with Java
  • 2. Java 102 • Object-oriented Programming – Fundamentals • Classes & Objects • Methods & Constructors • Encapsulation • Inheritance – Libraries & Clients
  • 3. Java 102: Intro to Object- oriented Programming with Java Fundamentals
  • 4. Classes • Java is an object-oriented language • Its constructs represent concepts from the real world. • Each Java program has at least one class that knows how to do certain actions or has properties • Classes in Java may have methods and properties (a.k.a. attributes or fields)
  • 6. Objects • Objects are created using Classes as a blueprint • Each object is an instance of a Class • Objects must be instantiated before they can be accessed or used in a program • Objects are instantiated using the new keyword
  • 7. Example 2: Creating Car Objects • These two Car instances are created with the new operator: Car car1 = new Car(); Car car2 = new Car(); • Now the variables car1 and car2 represent new instances of Car: car1.color=“blue”; car2.color=“red”;!
  • 8. Encapsulation public class Car { private String make; private String model; private String color; public void setColor(String color){ this.color = color; } public String getColor(){ return this.color; } void startEngine(){ System.out.println("starting engine"); } void stopEngine(){ System.out.println("stopping engine"); } variables store the state of the objects that are created from this class. Ideally. these should not be accessed directly by other objects getters and setters encapsulation the state of objects. All access to object variables should be done through this mechanism Methods encapsulate the behavior of objects created from this class
  • 9. Packages • Packages are used for grouping classes within a java project • Packages provide a way to organise code into cohesive groups • They are just folders on the filesystem • IDE’s replace the slashes with a dot as part of convention
  • 11. Functions • A sequence of program instructions that perform a specific task that are packaged as a unit • Takes zero or more input arguments. • Returns one output value. • May have side effects • Examples – Scientists use mathematical functions to calculate formulas. – Programmers use functions to build modular programs.
  • 12. Methods • Two types… – Class Level Methods – Object/instance level methods • Class level methods are referred to as static methods • Method declaration referred to as the signature – Method name – Parameter types • Examples. – Built-in static methods: Math.random(), Math.abs(), Integer.parseInt(). – I/O libraries: System.out.print(), – User-defined methods: main(), etc
  • 13. Benefits of Methods? • Methods enable you to build a new layer of abstraction. – Takes you beyond pre-packaged libraries. – You build the functionality you need • Process. – Step 1: identify a useful feature – Step 2: implement it – Step 3: use it (re-use it in any of your programs).
  • 14. Anatomy of a Java Method
  • 15. Scope • The block of code that can refer to that named variable • E.g. A variable's scope is code following in the block. • Best practice: declare variables to limit their scope
  • 17. Constructors • Constructors are special methods • They are called only once when the class is being instantiated: Tax t = new Tax(40000, “CA”,4); • They must have the same name as the class. • They can’t return a value and you don’t use void as a return type. public class Tax { // class variables / fields private double grossIncome; private String state; private int dependents; // Constructor public Tax (double grossIncome, String state, int depen){ // class variable initialization this.grossIncome = grossIncome; this.state = state; this.dependents = dependents; } }
  • 18. Method Overloading • Method overloading means having a class with more than one method having the same name but different argument lists. class LoanShark{ int calcLoanPayment(int amount, int numberOfMonths){ // by default, calculate for New York state calcLoanPayment(amount, numberOfMonths, “NY”); } int calcLoanPayment(int amount, int numberOfMonths, String state){ // Your code for calculating loan payments goes here } }
  • 20. Inheritance • Ability to define a new class based on an existing one. • E.g. lets create a James Bond Car from our existing Car class
  • 22. Method Overriding • If a subclass has the method with the same name and argument list, it will override (suppress) the corresponding method of its ancestor. • Method overriding comes handy in the following situations: • The source code of the super class is not available, but you still need to change its functionality • The original version of the method is still valid in some cases, and you want to keep it as is
  • 24. Classes and Objects Summary • A class declaration names the class and encloses the class body between braces • The class name can be preceded by modifiers e.g. public, private • The class body contains fields, methods, and constructors • A class uses fields to contain state information and uses methods to implement behaviour • Constructors that initialize a new instance of a class share its name and look like methods without a return type • Specify a class variable or a class method by using the static keyword in the member's declaration • Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference • You create an object from a class by using the new operator and a constructor • The garbage collector automatically cleans up unused objects
  • 25. Java 102: Intro to Object-oriented Programming with Java Java Libraries
  • 26. Definitions • Library - a module whose methods are primarily intended for use by many other programs. • Client - a program that calls a library. • API - the contract between client and implementation. • Implementation - a program that implements the methods in an API.
  • 27. Example: Standard Random • A library to generate pseudo-random numbers. Source: http://search.dilbert.com/comic/Tour%20Of%20Accounting
  • 29. Standard Random Implementation public class StdRandom { //between 0 and N-1 public static int uniform (int N){ return (int)(Math.random() * N); } // between lo and hi public static double uniform (double lo, double hi){ return lo + (int)(Math.random() * (hi-lo)); } // truth with probability public static boolean bernoulli(double p){ return Math.random() < p; } }
  • 31. Java Libraries Summary • Why use libraries? – Makes code easier to understand. – Makes code easier to debug. – Makes code easier to maintain and improve. – Makes code easier to reuse.
  • 32. Homework Exercise • Invent and program any sample application to illustrate inheritance. • For example, think of the classes Cat and Dog, Man and Woman, or a store inventory that has to be discounted...
  • 33. Further Reading • OO Concepts - http://docs.oracle.com/javase/tutorial/java/concepts/index.html • Classes and Objects – http://docs.oracle.com/javase/tutorial/java/javaOO/index.html • Interfaces and Inheritance - http://docs.oracle.com/javase/tutorial/java/IandI/index.html • Tools, tips and tricks for Unit Testing Java applications - http://www.junit.org