Object Oriented Programming Lecture-1: CSE 201 Conducted by Sarwar Morshed Email: Mobile: 01947179930

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

CSE 201

Conducted by
Sarwar Morshed
Email:
morshed.cse@green.edu.bd
Mobile: 01947179930
Object Oriented Programming
Lecture-1
Java Buzzwords
Simple
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted
High performance
Distributed
Dynamic

Features of OOP
Encapsulation
Encapsulation is a programming mechanism that
binds together code and the data it manipulates,
and that keeps both safe from outside
interference and misuse. In an object-oriented
language, code and data can be bound together
in such a way that a self-contained black box is
created. Within the box are all necessary data
and code. When code and data are linked
together in this fashion, an object is created. In
other words, an object is the device that supports
encapsulation.
Javas basic unit of encapsulation is the
class.


Features of OOP (Contd)
Polymorphism
Polymorphism (from Greek, meaning many forms) is
the quality that allows one interface to access a
general class of actions. The specific action is
determined by the exact nature of the situation. A
simple example of polymorphism is found in the
steering wheel of an automobile. The steering wheel
(i.e., the interface) is the same no matter what type of
actual steering mechanism is used. That is, the
steering wheel works the same whether your car has
manual steering, power steering, or rack-and-pinion
steering. Therefore, once you know how to operate
the steering wheel, you can drive any type of car.
More generally, the concept of polymorphism is often
expressed by the phrase one interface, multiple
methods.



Features of OOP (Contd)
Inheritance
Inheritance is the process by which one object
can acquire the properties of another object. This
is important because it supports the concept of
hierarchical classification.
For example, a Red Delicious apple is part of the
classification apple, which in turn is part of the
fruit class, which is under the larger class food.


Java Characteristics
Uses C/C++ basic syntax and basic data types -int,
char, float, double, long, short, byte etc.
Uses standard C/C++ control structures
Pure OO language
No stand alone functions -All code is part of a class
No explicit pointers - uses references
Uses garbage collection
Java is strongly typed
Java is normally compiled to a bytecode ;Java
bytecode is a machine language
for an abstract machine
Each platform (or browser) that runs Java has a Java
Virtual Machine (JVM or sometimes VM) . The JVM
executes Java bytecodes

Java The platform
Java has a large API (application programming
interface) covering a wide range of areas The
following list of Java APIs and applications from
Sun show the range of applications of Java . For
reference http://java.sun.com/products/index.html
Java Foundation Classes (JFC) - GUI
JDBC Database Access
JavaBeans - componentware
Java Web Server
Embedded Java - Java on embedded devices

Java IDE (Integrated Development
Environment)
Using JDK you can compile and run java program
from command line.
c:> javac HelloWorld. java - compiling here and it
will produce
HelloWorld.class i.e. bytecode.
c:>java HelloWorld - It runs java byte code on
native machine
Creating, Compiling, Debugging and Execution for
these four steps JDK is not user friendly. IDE is
provided for that. A list of IDEs are:
o Eclipse - from IBM
o Netbeans.

An Example HelloWorld
/**
This is my first java program
*/
public class Example
{
public static void main( String args[] )
{
System.out.println("Hello World");
}
}

Java Source Code Naming
Conventions
All java source file should end in .java
Each .java file can contain only one public class
The name of the file should be the name of the
public class plus ".java"
Do not use abbreviations in the name of the
class
If the class name contains multiple words then
capitalize the first letter of each
word ex. HelloWorld.java

CLASSPATH
Java uses the environment variable CLASSPATH
to locate class libraries .class files those needed
to compile or run the program it searches from
CLASSPATH
By default class path consists only the current
directory but you can provide them in classpath
option of javac or include directories in
CLASSPATH environment variables.
Example classpath = . ; c:\java\lib; c:\csi211\lab

Compiling the Program
To compile the Example program, execute the compiler,
javac, specifying the name of the source file on the
command line, as shown here:
javac Example.java
The javac compiler creates a file called Example.class that
contains the bytecode version of the program. Remember,
bytecode is not executable code. Bytecode must be
executed by a Java Virtual Machine. Thus, the output of
javac is not code that can be directly executed.
To actually run the program, you must use the Java
interpreter, java. To do so, pass the class name Example
as a command-line argument, as shown here:
java Example
When the program is run, the following output is displayed:
Hello World

The First Sample Program Line by Line
The next line of code in the program is shown
here:
class Example {
This line uses the keyword class to declare that a
new class is being defined. As mentioned, the
class is Javas basic unit of encapsulation.
Example is the name of the class. The class
definition begins with the opening curly brace ({)
and ends with the closing curly brace (}).

The First Sample Program Line by Line (Contd)
The next line of code is shown here:
public static void main (String args[]) {
This line begins the main( ) method, a subroutine is
called a method. As the comment preceding it
suggests, this is the line at which the program will
begin executing. All Java applications begin execution
by calling main( ).
The public keyword is an access modifier. An access
modifier determines how other parts of the program can
access the members of the class. When a class member is
preceded by public, then that member can be accessed by
code outside the class in which it is declared.
The keyword static allows main( ) to be called before
an object of the class has been created. This is
necessary because main( ) is called by the JVM
before any objects are made. The keyword void
simply tells the compiler that main( ) does not return a
value.


The First Sample Program Line by Line (Contd)
As stated, main( ) is the method called when a Java
application begins. Any information that you need to
pass to a method is received by variables specified
within the set of parentheses that follow the name of
the method. These variables are called parameters. If
no parameters are required for a given method, you
still need to include the empty parentheses.
In main( ) there is only one parameter, String args[ ],
which declares a parameter named args.
This is an array of objects of type String. (Arrays are
collections of similar objects.) Objects of type
String store sequences of characters. In this case,
args receives any command-line arguments present
when the program is executed.
The First Sample Program Line by Line (Contd)
The next line of code is shown here. Notice that it
occurs inside main( ).
System.out.println(HelloWorld.");
This line outputs the string "Java drives the Web."
followed by a new line on the screen. Output is
actually accomplished by the built-in println( )
method. In this case, println( ) displays the string
that is passed to it. As you will see, println( ) can be
used to display other types of information, too. The
line begins with System.out. System is a predefined
class that provides access to the system, and out is
the output stream that is connected to the console.
Thus, System.out is an object that encapsulates
console output.
All statements in Java end with a semicolon.



The First Sample Program Line by Line (Contd)
One last point: Java is case sensitive. Forgetting
this can cause you serious problems. For
example, if you accidentally type Main instead of
main, or PrintLn instead of println, the
preceding program will be incorrect. Furthermore,
although the Java compiler will compile classes
that do not contain a main( ) method, it has no
way to execute them. So, if you had mistyped
main, the compiler would still compile your
program. However, the Java interpreter would
report an error because it would be unable to find
the main( ) method.

Some Basic Java Syntax
class Syntax
{
public static void main( String args[] )
{
int aVariable = 5;
double aFloat = 5.8;
if ( aVariable < aFloat )
System.out.println( "True" );
int b = 10; // This is legal in Java
char c;
c = 'a';
}
}

Java Program Style and Layout
Style#1
class Syntax { // brace starts from here
public static void main( String args[] ) {
int aVariable = 5;
if ( aVariable < aFloat )
System.out.println( "True" );
}
}
Style #2
class Syntax
{ // brace starts
public static void main( String args[] )
{
int aVariable = 5;
if ( aVariable < aFloat )
System.out.println( "True" );
}
}
Naming conventions
Class Naming: Uses Capitalized word(s) i.e. Title
case
Examples:-
HelloWorld, MyList, StudentMark
Wrong:
helloWorld, HW (do not use abbreviation)
Variable and function names
-starts with a lowercase letter and after that use
Title case
Examples:-
variableAndFunctionNames
aFloat, studentName
Names of constants
All are capital letters and separated by underscore.
Example is
NAMES_OF_CONSTANTS

Naming conventions (contd)
Boolean
* true, false act like keywords but are boolean
constants
public class BooleanTest {
public static void main( String args[] ){
boolean flag = 2 > 3;
if ( flag && true)
System.out.println( "True" );
else
System.out.println( "False" );
}
}

Input and Output
Standard Java Output
-System.out is standard out in Java
-System.err is error out in Java
class Output
{
public static void main( String args[] )
{
// Standard out
System.out.print( "Prints, but no newline" );
System.out.println( "Prints, adds platforms newline at end"
);
double test = 4.6;
System.out.println( "You can use " + "the plus operator on
"+ test + " String mixed with numbers" );
System.err.println( "Standard error output" );
}
}

Input and Output (Contd)
Standard Java Input
Java assumes that you will be using a GUI for input from the user
Hence there is
- no "simple" way to read input from a user
Scanner class is in jdk 1.5
package lecture01;
import java.util.Scanner;
public class TestInput {
public static void main( String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.print("Write your name: ");
String userName = scanner.nextLine();
System.out.println("Your name is : " + userName);
}
}
Output
Write your name: Monzurur Rahman
Your name is : Monzurur RahmanYou typed:





*************** End of Lecture 1 **************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy