Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Outline

Object-Oriented Programming

The Java Programming Language

Program Development

1-1
Problem Solving
• The purpose of writing a program is to solve a
problem

• Solving a problem consists of multiple activities:


▪ Understand the problem
▪ Design a solution
▪ Consider alternatives and refine the solution
▪ Implement the solution
▪ Test the solution

• These activities are not purely linear – they


overlap and interact

1-2
Problem Solving
• The key to designing a solution is breaking it down
into manageable pieces

• When writing software, we design separate pieces


that are responsible for certain parts of the
solution

• An object-oriented approach lends itself to this


kind of solution decomposition

• We will dissect our solutions into pieces called


objects and classes

1-3
Object-Oriented Programming
• Java is an object-oriented programming language

• As the term implies, an object is a fundamental


entity in a Java program

• Objects can be used effectively to represent real-


world entities

• For instance, an object might represent a


particular employee in a company

• Each employee object handles the processing and


data management related to that employee

1-4
Objects
• An object has:
▪ state - descriptive characteristics

▪ behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its account


number and its current balance

• The behaviors associated with a bank account


include the ability to make deposits and
withdrawals

• Note that the behavior of an object might change


its state

1-5
Classes
• An object is defined by a class
• A class is the blueprint of an object
• The class uses methods to define the behaviors of
the object
• The class that contains the main method of a Java
program represents the entire program
• A class represents a concept, and an object
represents the example of that concept
• Multiple objects can be created from the same
class

1-6
Principle Of OOP

1-7
Encapsulation
• The word, “encapsulate,” means to enclose
something. Just like a pill "encapsulates" or contains
the medication inside of its coating, the principle of
encapsulation works in a similar way in OOP.
• In OOP, we encapsulate by binding the data and
functions which operate on that data into a single unit,
the class.
• By doing so, we can hide private details of a class
from the outside world and only expose functionality
that is important for interfacing with it.
• When a class does not allow calling code access to its
private data directly, we say that it is well
encapsulated.
1-8
1-9
Abstraction
• Applying abstraction means that each object
should only expose a high-level mechanism for using
it.
• This mechanism should hide internal implementation
details. It should only reveal operations relevant for the
other objects.

1-10
:real-life example of abstraction?
Think about how you use your phone

1-11
Inheritance
• It is the ability to acquire the properties of existing
classes and create new ones.
• Inheritance allows you to reuse code without having
to rewrite it in a program.
• One of the best features of Inheritance is the ability to
shorten the code in a program.
• You can use this principle to inherit codes from
another class and reuse it in a new class.

1-12
Types Of Inheritance
❑ Single inheritance
• In this inheritance, a
derived class is created
from a single base class.
• In the given example,
Class A is the parent class
and Class B is the child
class since Class B
inherits the features and
behavior of the parent
class A.

1-13
❑ Multi-level inheritance
• In this inheritance, a derived class is
created from another derived class.
• In the given example, class c inherits
the properties and behavior of class
B and class B inherits the properties
and behavior of class B. So, here A
is the parent class of B and class B
is the parent class of C. So, here
class C implicitly inherits the
properties and behavior of class A
along with Class B i.e there is a
multilevel of inheritance.

1-14
❑ Multiple inheritance
• In this inheritance, a derived class is created from
more than one base class. This inheritance is not
supported by .NET Languages like C#, F# etc. and
Java Language.
• In the given example, class c inherits the properties
and behavior of class B and class A at same level.
So, here A and Class B both are the parent classes
for Class C.

1-15
❑ Hierarchical Inheritance
• In this inheritance, more than one derived classes are
created from a single base class and further child
classes act as parent classes for more than one child
classes.
• In the given example, class A has two child's class B
and class D. Further, class B and class C both are
having two child's - class D and E; class F and G
respectively.

1-16
❑ Hybrid inheritance
• This is combination of more than one inheritance.
Hence, it may be a combination of Multilevel and
Multiple inheritance or Hierarchical and Multilevel
inheritance or Hierarchical and Multipath inheritance or
Hierarchical, Multilevel and Multiple inheritance.
• Since .NET Languages like C#, F# etc. does not
support multiple and multipath inheritance. Hence
hybrid inheritance with a combination of multiple or
multipath inheritances is not supported by .NET
Languages.

1-17
1-18
Polymorphism
• Polymorphism refers to one name with many forms.
• It is the ability of one function to perform in different
ways.
• In other words, it refers to an object’s ability to take on
more than one single form.
• Polymorphism can be applied in two simple ways
➢ Method Overloading
➢ Method Overriding

1-19
❑ Method Overloading

• When a class has multiple methods with the same


names but a different set of parameters, it is called
Method overloading.
• You can proceed with method overloading, only if your
methods satisfy any one of the following rules -
➢ Has different parameters
➢ Has different return types
➢ Has different access modifiers.

1-20
• Here is an example of Method Overloading

class coding{
public void code() {
System.out.println("Coding in C++");
}
public void code(String language) {
System.out.println("Coding in "+language);
}
}

1-21
❑ Method Overriding
• The second way to go ahead with polymorphism is
method overriding.
• This is only possible if a subclass ( or ) sister class has
the same method as the parent class. Much like
Method overloading,
• there are also some rules for method overriding to
work.
➢ Has the same parameter list
➢ Has the same return type
➢ And should have an access modifier that more
restrictive than that of the parent class.

1-22
Here is an example of method overriding -

public class coding {


public void code() {
​ System.out.println("Coding in C++");
}
}
public class JavaProgrammer extends coding{
public void code() {
​ System.out.println("Coding in Java");
}
}
1-23
Outline
Object-Oriented Programming

The Java Programming Language

Program Development

1-24
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

• These terms will be explored in detail throughout


the course

• A Java application always contains a method


called main

1-25
Example 1.java
public class Example
{
//-----------------------------------------------------------------
// Prints a presidential quote.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("Whatever you are, be a good one.");
}
}

1-26
Java Program Structure
// comments about the class
public class MyProgram
{
class header

class body

Comments can be placed almost anywhere


}

1-27
Java Program Structure
// comments about the class
public class MyProgram
{

// comments about the method


public static void main (String[] args)
{
method header
method body
}

1-28
Comments
• Comments in a program are called inline
documentation
• They should be included to explain the purpose
of the program and describe processing steps
• They do not affect how a program works
• Java comments can take three forms:
// this comment runs to the end of the line

/* this comment runs to the terminating


symbol, even across line breaks */

/** this is a javadoc comment */

1-29
Identifiers
• Identifiers are the words a programmer uses in a
program
• An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign
• Identifiers cannot begin with a digit
• Java is case sensitive - Total, total, and
TOTAL are different identifiers
• By convention, programmers use different case
styles for different types of identifiers, such as
▪ title case for class names - Example
▪ upper case for constants - MAXIMUM

1-30
Name conventions

• Java is case-sensitive; maxval, maxVal, and MaxVal


are three different names
• Class names begin with a capital letter
• All other names begin with a lowercase letter
• Subsequent words are capitalized: theBigOne
• Underscores are not used in names
• These are very strong conventions!
Reserved Words
• The Java reserved words:
abstract else interface switch
assert enum long synchronized
boolean extends native this
break false new throw
byte final null throws
case finally package transient
catch float private true
char for protected try
class goto public void
const if return volatile
continue implements short while
default import static
do instanceof strictfp
double int super

1-32
White Space
• Spaces, blank lines, and tabs are called white
space
• White space is used to separate words and
symbols in a program
• Extra white space is ignored
• A valid Java program can be formatted many ways
• Programs should be formatted to enhance
readability, using consistent indentation

1-33
Outline
Object-Oriented Programming

The Java Programming Language

Program Development

1-34
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

1-35
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 which 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

1-36
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

• Java is considered to be architecture-neutral

1-37
Java Virtual Machine

• The .class files generated by the compiler are not


executable binaries
▪ so Java combines compilation and interpretation
• Instead, they contain “byte-codes” to be executed by
the Java Virtual Machine
• This approach provides platform independence, and
greater security
Java Translation
Java source
code Java
bytecode

Java
compiler
Bytecode Bytecode
interpreter compiler

Machine
code

1-39
Development Environments
• There are many programs that support the
development of Java software, including:
▪ Sun Java Development Kit (JDK)
▪ Sun NetBeans
▪ IBM Eclipse
▪ Borland JBuilder
▪ MetroWerks CodeWarrior
▪ BlueJ
▪ jGRASP

• Though the details of these environments differ,


the basic compilation and execution process is
essentially the same

1-40
Syntax and Semantics
• The syntax rules of a language define how we can
put together symbols, reserved words, and
identifiers to make a valid program

• The semantics of a program statement define what


that statement means (its purpose or role in a
program)

• A program that is syntactically correct is not


necessarily logically (semantically) correct

• A program will always do what we tell it to do, not


what we meant to tell it to do

1-41
Errors
• A program can have three types of errors

• The compiler will find syntax errors and other


basic problems (compile-time errors)
▪ If compile-time errors exist, an executable version of the
program is not created

• A problem can occur during program execution,


such as trying to divide by zero, which causes a
program to terminate abnormally (run-time errors)

• A program may run, but produce incorrect results,


perhaps using an incorrect formula (logical errors)

1-42
Basic Program Development

Edit and
save program
errors

errors
Compile program

Execute program and


evaluate results

1-43

You might also like