Professional Documents
Culture Documents
Week 1 Int To Java 2
Week 1 Int To Java 2
Object-Oriented Programming
Program Development
1-1
Problem Solving
• The purpose of writing a program is to solve a
problem
1-2
Problem Solving
• The key to designing a solution is breaking it down
into manageable pieces
1-3
Object-Oriented Programming
• Java is an object-oriented programming language
1-4
Objects
• An object has:
▪ state - descriptive characteristics
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
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 -
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
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
1-27
Java Program Structure
// comments about the class
public class MyProgram
{
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
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
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
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)
1-35
Programming Languages
• Each type of CPU executes only a particular
machine language
1-36
Java Translation
• The Java compiler translates Java source code into a
special representation called bytecode
1-37
Java Virtual Machine
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
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
1-41
Errors
• A program can have three types of errors
1-42
Basic Program Development
Edit and
save program
errors
errors
Compile program
1-43