0% found this document useful (0 votes)
4 views

OOP-Lec21-Java Introduction

The document provides an introduction to Object-Oriented Programming (OOP) in Java, covering fundamental concepts such as classes, objects, inheritance, and polymorphism. It outlines the characteristics of Java, including its simplicity, robustness, and portability, as well as the architecture of Java applications involving the Java Virtual Machine (JVM) and Java Development Kit (JDK). Additionally, it discusses the structure of Java programs, the development process, and examples of basic Java syntax and operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOP-Lec21-Java Introduction

The document provides an introduction to Object-Oriented Programming (OOP) in Java, covering fundamental concepts such as classes, objects, inheritance, and polymorphism. It outlines the characteristics of Java, including its simplicity, robustness, and portability, as well as the architecture of Java applications involving the Java Virtual Machine (JVM) and Java Development Kit (JDK). Additionally, it discusses the structure of Java programs, the development process, and examples of basic Java syntax and operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Object Oriented Programming

(CS-112)

Instructor: Dr. Tufail Muhammad

OOP in Java - Introduction

Department of Computer Science


Air University - Aerospace & Aviation Campus, Kamra
Contents
• Recap
• Java Programming
Recap
Recap
• Basic OOP Concepts
• Objects & Classes
• Constructor and Destructor
• Const Members & Member functions
• Static Members & Member functions
• Inheritance
• Polymorphism
• Friend functions & Friend classes
• Operator Overloading
• Generic Programming
• Type Conversion (for class type)
Java
Java Programming Language
• A programming language specifies the words and symbols that we
can use to write a program
• A programming language employs a set of rules that dictate how the
words and symbols can be put together to form valid program
statements
• The Java programming language was created by Sun Microsystems
Inc.
• It was introduced in 1995 and its popularity has grown quickly since
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
Characteristics of Java
• Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
• Java Is Object-Oriented Java as "C++--" because it is like C++ but
• Java Is Distributed with more functionality and fewer negative
aspects.
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is
Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple Java is inherently object-oriented.
Although many object-oriented languages
• Java Is Object-Oriented began strictly as procedural languages,
• Java Is Distributed Java was designed from the start to be
object-oriented. Object-oriented
• Java Is Interpreted programming (OOP) is a popular
• Java Is Robust programming approach that is replacing
traditional procedural programming
• Java Is Secure techniques.
• Java Is
Architecture-Neutral One of the central issues in software
development is how to reuse code.
• Java Is Portable Object-oriented programming provides
• Java's Performance great flexibility, modularity, clarity, and
reusability through encapsulation,
• Java Is Multithreaded inheritance, and polymorphism.
• Java Is Dynamic
Characteristics of Java
• Java Is Simple Distributed computing involves several
computers working together on a network.
• Java Is Object-Oriented Java is designed to make distributed
• Java Is Distributed computing easy. Since networking
capability is inherently integrated into
• Java Is Interpreted Java, writing network programs is like
• Java Is Robust sending and receiving data to and from a
file.
• Java Is Secure
• Java Is
Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple You need an interpreter to run Java
programs. The programs are compiled into
• Java Is Object-Oriented the Java Virtual Machine code called
• Java Is Distributed bytecode. The bytecode is
machine-independent and can run on any
• Java Is Interpreted machine that has a Java interpreter, which
• Java Is Robust is part of the Java Virtual Machine (JVM).
• Java Is Secure
• Java Is
Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple Java compilers can detect many problems
that would first show up at execution time
• Java Is Object-Oriented in other languages.
• Java Is Distributed
Java has eliminated certain types of
• Java Is Interpreted error-prone programming constructs found
• Java Is Robust in other languages.
• Java Is Secure Java has a runtime exception-handling
• Java Is feature to provide programming support
Architecture-Neutral for robustness.
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
Java implements several security
• Java Is Robust mechanisms to protect your system against
• Java Is Secure harm caused by stray programs.

• Java Is
Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Write once, run anywhere
Architecture-Neutral
With a Java Virtual Machine (JVM),
• Java Is Portable you can write one program that will
• Java's Performance run on any platform.

• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is
Architecture-Neutral Because Java is architecture neutral,
• Java Is Portable Java programs are portable. They can
be run on any platform without being
• Java's Performance recompiled.
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is
Architecture-Neutral Java’s performance Because Java is
• Java Is Portable architecture neutral, Java programs are
portable. They can be run on any
• Java's Performance platform without being recompiled.
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is
Architecture-Neutral
• Java Is Portable
Multithread programming is smoothly
• Java's Performance integrated in Java, whereas in other
• Java Is Multithreaded languages you have to call procedures
specific to the operating system to enable
• Java Is Dynamic multithreading.
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is
Architecture-Neutral
• Java Is Portable Java was designed to adapt to an evolving
environment. New code can be loaded on the
• Java's Performance fly without recompilation. There is no need for
developers to create, and for users to install,
• Java Is Multithreaded major new software versions. New features can
• Java Is Dynamic be incorporated transparently as needed.
Java Architecture
✅ Java Virtual Machine (JVM)

• Abstract machine that enables Java bytecode to run on any platform.

• Converts .class (bytecode) to machine code.

• Performs Just-In-Time (JIT) compilation for performance.

✅ Java Runtime Environment (JRE)

• Contains JVM + libraries required to run Java applications.

• Does not include development tools.

✅ Java Development Kit (JDK)

• Full development suite: includes JRE + compiler + debugger + tools like javac,
javadoc, jar
JDK Editions
• Java Standard Edition (J2SE)
• J2SE can be used to develop client-side standalone
applications or applets.

• Java Enterprise Edition (J2EE)


• J2EE can be used to develop server-side applications such as
Java servlets, Java ServerPages, and Java ServerFaces.

• Java Micro Edition (J2ME)


• J2ME can be used to develop applications for mobile devices
such as cell phones.
Popular Java IDEs
• Eclipse
• NetBeans
• IntelliJ IDEA
• DrJava
Java Program Structure
• In the Java programming language:
• A program is made up of one or more classes
• A class contains one or more methods
• A method contains program statements
Program Development
• The mechanics of developing a program include several activities
• writing the program in a specific programming language (such as Java)
• translating the program into a form that the computer can execute
• investigating and fixing various types of errors that can occur

• Software tools can be used to help with all parts of this process
Programming Languages
• Each type of CPU executes only a particular machine language
• A program must be translated into machine language before it can be
executed
• A compiler is a software tool that translates source code into a specific
target language
• Often, that target language is the machine language for a particular CPU
type
• The Java approach is somewhat different
Java Translation
• The Java compiler translates Java source code into a special
representation called bytecode
• Java bytecode is not the machine language for any traditional CPU
• Another software tool, called an interpreter, translates bytecode into
machine language and executes it
• Therefore, the Java compiler is not tied to any particular machine
Creating, Compiling, and
Running Programs
What is Eclipse?
• Best known as an Integrated Development Environment (IDE)
• Provides tools for coding, building, running, and debugging applications

• Eclipse started as IBM product

• Open Source
• It is a general purpose open platform that facilitates and encourages the development of
third-party plug-ins

• Originally designed for Java, now supports many other languages


• Good support for C, C++
• Python, PHP, Ruby, etc…
Prerequisites for Running Eclipse

• Eclipse is written in Java and will thus need an installed JDK in


which to execute
Selecting a Workspace
• In Eclipse, all of your code will live under a workspace

• A workspace is nothing more than a location where we will store our source code and
where Eclipse will write out our preferences

• Choose a location where you want to store your files, then click OK
Eclipse Environment
Eclipse
Environment
Eclipse Environment
Eclipse Environment
Eclipse Environment
Eclipse Environment
HelloWorld Program
HelloWorld Program
HelloWorld Program
Font Size
Outline
Warning
1 // Fig. 2.1: Welcome1.java Outline
2 // A first program in Java.
3
4 public class Welcome1 {
5 Welcome1.java
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome to Java Programming!" );
10
11 } // end method main
12
13 } // end class Welcome1

Welcome to Java Programming! Program Output

© 2002 Prentice Hall.


All rights reserved.
1 // Fig. 2.3: Welcome2.java Outline
2 // Printing a line of text with multiple statements.
3
4 public class Welcome2 {
5 Welcome2.java
6 // main method begins execution of Java application
7 public static void main( String args[] ) 1. Comments
8 {
9 System.out.print( "Welcome to " );
10 System.out.println( "Java Programming!" ); 2. Blank line
11
12 } // end method main
13 3. Begin class Welcome2
14 } // end class Welcome2
3.1 Method main

System.out.print keeps the cursor on 4. Method


the same line, so System.out.println System.out.print
continues on the same line.
4.1 Method
System.out.println

5. end main, Welcome2

Program Output

Welcome to Java Programming!

© 2002 Prentice Hall.


All rights reserved.
Data Types in Java Example
• int i = 63;
int j = 35;
System.out.println(i / j);
Output: 1

• double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8

• Almost same as in studied in C++

© 2002 Prentice Hall. All rights reserved.


Operator
• There are 5 different groups of operators:

– Arithmetic operators
– Assignment operator
– Increment/Decrement operators
– Relational operators
– Conditional operators

© 2002 Prentice Hall. All rights reserved.


Example
Java performs short-circuit evaluation:
It evaluates && and || expressions from left to right and once it finds the result, it stops.

public static void main(String[] args) {


int a=5;
int b=2;
if(a>2 || (b++ > 1)){
System.out.print(b);
}

public static void main(String[] args) {


int a=5;
int b=2;
if(a>2 && (b++ > 1)){
System.out.print(b);
}

© 2002 Prentice Hall. All rights reserved.

You might also like