Lect 0ne

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

CP 215

OBJECT ORIENTED PROGRAMMING


IN JAVA
CP 215
• GROUP 1 : TUESDAY (11:00-13:00) LRB 105 IDIT,
MTA,BIS

• GROUP 2 : Thursday (10:00-12:00) LRB 105 CE,


TE, SE, HIS

• GROUP 3: Friday(10:00-12:00) LRB105 CNISE,


CSDFE, IS, CS, DCBE
Assesment Model
• Course works 40%
• University Exams 60%
• Any kind of cheating will result to discontinue
from study With 0 tolerance.
outline
• Outline
Overview

• Java programming language was originally


developed by Sun Microsystems which was
initiated by James Gosling and released in
1995 as core component of Sun Microsystems'
Java platform (Java 1.0 [J2SE])
• Java runs on a variety of platforms, such as
Windows, Mac OS, and the various versions of
UNIX.
Java is −

• Object Oriented − In Java, everything is an


Object.
• Java can be easily extended since it is based
on the Object model.
Java is −
• Platform Independent − Unlike many other
programming languages including C and C++,
when Java is compiled, it is not compiled into
platform specific machine, rather into
platform independent byte code.
• This byte code is distributed over the web and
interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
Java is −
• Simple − Java is designed to be easy to learn.
If you understand the basic concept of OOP
Java, it would be easy to master.
Java is −
• Secure − With Java's secure feature it enables
to develop virus-free, tamper-free systems.
Authentication techniques are based on
public-key encryption.
Java - Basic Syntax

• When we consider a Java program, it can be


defined as a collection of objects that
communicate via invoking each other's
methods.
• In the following slides, briefly look into what
do class, object, methods, and instance
variables mean
Java - Basic Syntax

• Object − Objects are entities in a software system


which represent instances of real-world and system
entities
• Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behavior
such as wagging their tail, barking, eating.
• An object is an instance of a class.
• Refer to Object-oriented Design: Designing systems
using self-contained objects and object classes
Java - Basic Syntax

• An object is an entity which has a state and a defined set of


operations which operate on that state. The state is
represented as a set of object attributes. The operations
associated with the object provide services to other objects
(clients) which request these services when some
computation is required.

• Objects are created according to some object class


definition. An object class definition serves as a template for
objects. It includes declarations of all the attributes and
services which should be associated with an object of that
class.
Refer to Object Oriented Software Design

• Characteristics of OOD
 Objects are abstractions of real-world or system entities
and manage themselves
 Objects are independent and encapsulate state and
representation information.
 System functionality is expressed in terms of object
services
 Shared data areas are eliminated.
 Objects communicate by message passing
 Objects may be distributed and may execute
sequentially or in parallel
Java – Basics of objects

• Interacting objects
o1: C1 o3:C3 o4: C4
state o1 state o3 state o4
ops1() ops3 () ops4 ()

o2: C3 o6: C1 o5:C5


state o2 state o6 state o5
ops3 () ops1 () ops5 ()
The Unified Modelling Language
• Several different notations for object-oriented
designs were proposed in the 1980s describing
and 1990s
• The Unified Modeling Language is an
integration of these notations
• It describes notations for a number of different
models that may be produced during OO
analysis and design
• It is now a de facto standard for OO modelling
The Unified Modeling Language
• Employee object class (UML)
Employee
name: string
address: string
dateOfBirth: Date
employeeNo: integer
socialSecurityNo: string
department: Dept
manager: Employee
salary: integer
status: {current, left, retired}
taxCode: integer
. ..
join ()
leave ()
retire ()
changeDetails ()
Class
• A class can be defined as a template/blueprint
that describes the behavior/state that the
object of its type supports.
• classes may inherit attributes and services
from other classes.
Methods
• A method is basically a behavior. A class can
contain many methods.
• It is in methods where the logics are written,
data is manipulated and all the actions are
executed.
Instance Variables
• Each object has its unique set of instance
variables. An object's state is created by the
values assigned to these instance variables.
First Java Program
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output */
public static void main(String []args)
{ System.out.println("Hello World");
// prints Hello World
}}
First Java Program
• Java class declarations normally contain one or
more methods. For a Java application, one of
the methods must be called main and must be
defined as shown in in our first program;
otherwise, the Java Virtual Machine (JVM) will
not execute the application.
• Methods perform tasks and can return
information when they complete their tasks.
public static void main(String[] args)
First Java Program
it is very important to keep in mind the following
points.
• Case Sensitivity − Java is case sensitive, which
means identifier Hello and hello would have
different meaning in Java.
• Class Names − For all class names the first letter
should be in Upper Case. If several words are used
to form a name of the class, each inner word's first
letter should be in Upper Case.
Example: class MyFirstJavaClass
First Java Program
it is very important to keep in mind the
following points.
• Method Names − All method names should
start with a Lower Case letter. If several words
are used to form the name of the method,
then each inner word's first letter should be in
Upper Case.
• Example: public void myMethodName()
First Java Program
it is very important to keep in mind the following points.
• Program File Name − Name of the program file should
exactly match the class name.
• When saving the file, you should save it using the class
name (Remember Java is case sensitive) and append
'.java' to the end of the name (if the file name and the
class name do not match, your program will not
compile).
• Example: Assume 'MyFirstJavaProgram' is the class
name. Then the file should be saved
as 'MyFirstJavaProgram.java'
First Java Program
it is very important to keep in mind the
following points.
• public static void main(String args[]) − Java
program processing starts from the main()
method which is a mandatory part of every
Java program
Java Identifiers

• All Java components require names. Names


used for classes, variables, and methods are
called identifiers.
Java Identifiers

In Java, there are several points to remember


about identifiers. They are as follows −
• ll identifiers should begin with a letter (A to Z
or a to z), currency character ($) or an
underscore (_).
• After the first character, identifiers can have
any combination of characters.
• A key word cannot be used as an identifier.
Java Identifiers

In Java, there are several points to remember


about identifiers. They are as follows −
• Most importantly, identifiers are case
sensitive.
• Examples of legal identifiers: age, $salary,
_value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.
Java Keywords

• The following list shows the reserved words in


Java. These reserved words may not be used
as constant or variable or any other identifier
names.
Java Keywords
Comments in Java

• Java supports single-line and multi-line


comments very similar to C and C++.
• All characters available inside any comment
are ignored by Java compiler.
/* This is my first java program.
* This will print 'Hello World' as the output
• This is an example of multi-line comments. */
// This is an example of single line comment
Java Modifiers

• Access modifiers help you set the level of access you


want for your Class, variables as well as Methods.
• Like other languages, it is possible to modify classes,
methods, etc., by using modifiers. There are two
categories of modifiers −
• Access Modifiers − default, public , protected, private
• Non-access Modifiers − final, abstract, strictfp
More detail can be found here:
http://javabeginnerstutorial.com/core-java-tutorial/access-modifier-in-
java/
Java Modifiers

• Public: When set to public, the given Class will


be accessible to all the classes available in the
Java world.
• Protected: If a variable is set to protected
inside a Class, it will be accessible from its sub
classes defined in the same or different
package only via Inheritance.
Java Modifiers

• Private: A variable if defined private will be


accessible only from within the Class in which
it is defined. Such variables are not accessible
from outside the defined Class, not even in its
subclass .
Java Access Modifiers Table for Variable
Java Access Modifiers Table for Method
Java Variables

• Following are the types of variables in Java −


• Local Variables: Variables that are declared
inside Methods in a Java program are called
local variables.
• Class Variables (Static Variables)
• Instance Variables (Non-static Variables)
Instance variables
• Instance variables are used by Objects to store
their states.
• Variables which are defined without
the STATIC keyword and are Outside any
method declaration are Object specific and are
known as instance variables.
• They are called so because their values are
instance specific and are not shared among
instances.
Instance variables
• Example of Instance Variable
class Page {
public String pageName;
// instance variable with public access
private int pageNumber;
// instance variable with private access
}
Displaying outputs
• Java provide different methods of displaying
output of the program to the screen.
• Print
• Println
• Printf

• System.out.print();
Escape Sequence
Memory Concepts
• Variable names correspond to locations in the
computer’s memory. Every variable has a name,
a type, a size (in bytes) and a value.
• Eg: number1 = input.nextInt(); // read the
number from the user.
• Scanner class enables program to read data
from the user.
• Its declaration:
• Scanner input= new Scanner(System.in)

You might also like