You are on page 1of 36

Basic OOP and

Introduction to Java
1
Basic Object-Oriented
Programming (OOP)

2
Object-Oriented Programming

Object-Oriented Programming
 Focuses on objects and classes based on real world scenarios
 Emphasizes state, behavior, and interaction of objects
 Advantages
- Faster development
- Increased quality
- Easier maintenance
- Enhanced modifiability
- Increased software reuse 3
Classes and Objects

Class
 collection of objects is called class.
 can also be define as blueprint
 doesn't consume any space.
Ex:
Vehicle

4
Classes and Objects (cont…)

Object
 any entity that has state and behavior is known as an object
 can be physical or logical
 consumes space
Ex:
Car is an instance of Vehicle

5
Classes and Objects (cont…)

CLASS OBJECT
Brand Brand Lamborghini

Model Model Aventador

Color Color Gold


6
2
Introduction to Java

7
What is Java?

Java
 programming language that is class-based
 high-level programming language
 fundamentals of Java came from a programming language called C++.

8
History of Java

James Gosling
 Originally developed Java at Sun Microsystems, a company now owned by
Oracle.

Oak and Green


 Java initially called Oak after an oak tree that stood outside Gosling’s
office.
 Later the project went by the name Green and was finally renamed Java,
from Java Coffee.
9
3
Java Program Structure

10
Structure of Java Program

Documentation Section
 You can write a comment in this section. Comments are beneficial for the
programmer because they help them understand the code. These are
optional, but we suggest you use them because they are useful to
understand the operation of the program, so you must write comments
within the program.

11
Structure of Java Program (cont…)

Package Statement
 You can create a package with any name. A package is a group of classes
that are defined by a name. That is, if you want to declare many classes
within one element, then you can declare it within a package. It is an
optional part of the program, i.e., if you do not want to declare any
package, then there will be no problem with it, and you will not get any
errors. Here, the package is a keyword that tells the compiler that package
has been created.

12
Structure of Java Program (cont…)

Import Statements
 This line indicates that if you want to use a class of another package, then
you can do this by importing it directly into your program.

13
Structure of Java Program (cont…)

Interface Statement
 Interfaces are like a class that includes a group of method declarations.
It's an optional section and can be used when programmers want to
implement multiple inheritances within a program.

14
Structure of Java Program (cont…)

Class Definition
 A Java program may contain several class definitions. Classes are the
main and essential elements of any Java program.

15
Structure of Java Program (cont…)

Main Method Class


 Every Java stand-alone program requires the main method as the starting
point of the program. This is an essential part of a Java program. There
may be many classes in a Java program, and only one class defines the
main method. Methods contain data type declaration and executable
statements.

16
Structure of Java Program (cont…)

Documentation Section Suggested

Packages Statement Optional

Import Statement Essential

Interface Statements Optional

Class Definitions Optional

Main-Method Class Compulsory

17
4
Classes and Methods

18
Classes and Methods

Class
 This is the basic structure of the object-
oriented programming
 Can be define as blueprint or a template
 Made up of attributes and behavior

19
Classes and Methods (cont…)

Vehicle class name


vehicleID
brand attributes/variables
model
color
engineOn()
engineOff() behaviors/methods
run()
stop()

20
Classes and Methods (cont…)

Defining a class

21
Classes and Methods (cont…)

Method
 Collection of statements that grouped
together to perform an operation
 Specifies the behavior(s) of the class and
is define within the class
 May or may not be bounded by a return
statement

22
Classes and Methods (cont…)

Defining a method

23
5
Syntax and Runtime Error

24
Syntax and Runtime Error

Syntax Error
 A character or string incorrectly placed in a
command or instruction that causes a failure in
execution
 These problem may cause unplaced command
or instruction.
-Placing “;” (semicolon) on the end of the
statement
25
Syntax and Runtime Error (cont…)

Runtime Error
 A program error that occurs while program is
running, may or may not cause program to stop
 Also called logic error
 Some error that do not cause the program to
stop are:
-Infinite loops
-Programs with wrong outputs 26
Syntax and Runtime Error (cont…)

Runtime Error
 A program error that occurs while program is
running, may or may not cause program to stop
 Also called logic error
 Some error that do not cause the program to
stop are:
-Infinite loops
-Programs with wrong outputs 27
6
Java Program
Statements

28
Java Program Statements

Java Program Statement


 Inside the main() method or other methods
there are statements that make up the body of
the program. These statements are executed
when the program runs.
 Java program statements are terminated by a
“;” (semicolon)

29
7
Java Comments

30
Java Comments

Comments
 These are notes written to a code for documentation
purposes.
 Those texts are not part of the program and does not
affect the flow of the program.

31
Java Comments (cont…)

3 Types Comments in Java


 C++ Style Comments
 C Style Comments
 Special Javadoc Comments

32
Java Comments (cont…)

C++ Style Comments


 C++ Style comments starts with //
 All the text after // are treated as comments

33
Java Comments (cont…)

C-Style Comments
 C-Style comments starts with a /* and ends with a */
 All the text between the two delimiters are treated as
comments

34
Java Comments (cont…)

Special Javadoc Comments


 Special Javadoc comments are used to generating an
HTML documentation for your Java Programs
 You can create Javadoc comments by starting the line
with /** and endint it in */
 It can also contain certain tags to add more information
to your comments

35
Java Comments (cont…)

Special Javadoc Comments

36

You might also like