Object Oriented Programming in Java
Object Oriented Programming in Java
Object Oriented Programming in Java
College of Computing
Department of Information
Systems
Short not manual for Exit exam (Module)
January, 2023
Course Title Object Oriented Programming with Java
Module Title Advanced Programming
Module Code ITec-M2051 Course Code: ITech2052
CP/ECTS 7
Study Hours Lecture: - 32 Laboratory: Tutorial: - Home Study: 71 Total:135
48 32
Prerequisite(s): Fundamentals of Programming II Course Code: ITec2042
Course This course will cover almost all lab practice of Object Oriented
Description Programming with Java using NetBeans 8.0, the concept of class and
object, inheritance, abstraction, polymorphism and exception handling....
will be discussed widely.
Learning At the end of this course, Students will be able to:
Outcomes • Understand the concept of Object Oriented Programming
• Differentiate class and object
• Understand Exception Handling mechanism
• Do Simple Project
Chapter One: Object Oriented The Java platform consists of:
Programing the Java VM
1.1. Overview the Java API
Object oriented programming: allow the (Application
programmer to break down the problem into Programming
objects. Interface).
Objects: self-contained entities consisting of The API provides libraries, which
both data and operations together. groups a number of related classes
and interfaces together into what is
E.g. java, C++, C#, VB6, Python…
called a Java package.
OOP: is a programming language model
organized around objects rather than 1.2. Fundamentals of Objects and
"actions" and data rather than “logic”. Classes
Programming Paradigms፡
Java technology consists of an OO
1. Procedural Programming:
programming language and a platform on
(remember)
which programs can be run.
is a list of step-by-step
The Java compiler translates the program
instructions
into an intermediate language called Java
bytecodes. usually having a linear order of
execution.
Java bytecodes are platform-independent.
Includes C, C++, FORTRAN,
Guaranteed to be Write Once, Run
Pascal, and Basic.
Anywhere.
2. Object oriented programming:
There are different Java interpreters for
different platforms. allow the programmer to break
down the problem into objects:
Java is: + Object Oriented + Platform
Independent Easy to debugging.
+ Simple + Secure E.g. java, C++, C#, VB6, Python
+ Portable + 3. Declarative:
Multithreaded + Robust
Describe a problem rather than
Q1: What is the d/c between compiler and defining a solution
interpreter?
Describes what something is like,
The Java platform is a bit different to rather than how to create it.
other platforms, in that it consists of
software only – that means it is ፩. Objects: (Recall)
hardware-independent. Are instances of a class
Are the basic runtime entities in ፭. Encapsulation:
an OO program.
means the wrapping up of data and
Are identifiable things or methods into a single unit (a class).
individuals?
It also means that the data inside a
Are of significance to the system class is hidden from everything
outside the class.
Have states reflected in the
attributes (data) held about The data can only be accessed by
the object invoking the methods of the class.
Have characteristic behaviour It is placing the data and the
(i.e. functionality) functions that work on that data in
the same place.
፪. Class:
፮. Inheritance:
Is a blueprint for an object.
It is the way in which objects of one
is the definition of a set of objects
class get the properties of objects of
which are all similar in terms of their another class(data &methods).
attributes
For example, Person, employee and
A class consists of a class name, data retiree
and methods.
Inheritance provides the idea of
e.g. customer class defines all reusability – the Person class can be
customer objects. reused without making changes to it,
because another class can be made to
፫. Members: inherit from it.
Collectively, the methods and Types of Inheritance? read
variables defined within a class are
called members of the class. ፯. Polymorphism:
These are local, instance and The word polymorphism means the
Class/static variable ability to take more than one form.
Type casting
Assigning a value of one type to a Syntax: smallerDT
variable of another type is known as var=(smallerDT)largerDTVar
Type Casting. OR largerValue;
In Java, type casting is classified into Example: float y=78.9f; byte
two types b = (byte)y; //explicit type
casting required
1. Widening, (Implicit) or
Automatic Casting: Java int x = (int)2.7; double d =
supports automatic widening, 100.04; long l = (long)d;
a kind of conversion from Question: What is the merit
one type to another which and demerit of implicit and
doesn’t lose information. explicit type casting?
Boxing and Unboxing:
Autoboxing is the automatic
conversion that the Java compiler
makes between the primitive types
Two conditions for automatic and their corresponding object
casting wrapper classes.
Types are compatible
For example, converting an int to an 3. Control flow statements: regulates
Integer, a double to a Double, and so the order of statements execution
on.
Selection: the if() and switch()
If the conversion goes the other way, statements
this is called unboxing.
Iteration: The for(), while() and
Converting primitive data types into do…while() statements
object is called boxing.
4. Jumping Statements: to jump out
Therefore, while using a wrapper of loops and to control other areas of
class you just need to pass the value program java uses break and
of the primitive data type to the continue statements.
constructor of the Wrapper class. The break statement: used to exit a
loop
And the Wrapper object will be
The Continue Statement: used to
converted back to a primitive data
force program to go back to the top
type, called unboxing.
of a loop
The Number class is part of the Labels: Java does not include a goto
java.lang package. statement.
Instead of goto, Java allows you to
Overview of Java statements combine break and continue with a
Statements are roughly equivalent to label.
sentences in natural languages.
A statement forms a complete unit of
execution.
There are three kinds of statements:
1. Expression statements: Perform
computations and return values. It is
a series of variables, operators, and
method calls that evaluates to a
single value. (constructed according
to the syntax of the language). There
are:
A. Assignment expressions,
C. Any use of
++ or -- ,
B. Method calls,
D. Object
creation expressions ,
2. Declaration statements: declares/
Introduces a very first variables E.g.
double c = 8.4;
Chapter Two: Classes and Thus, the data for one object is
separate and unique from the data for
Objects another.
2.1. Fundamentals of Classes
All methods have the same general
A class is a blue print from which
form as main( )
individual objects are created.
However, most methods will not be
Class is a template for an object, and
specified as static or public.
an object is an instance of a class
Java classes do not need to have a
Class is the logical construct upon
main( ) method except the starting
which the entire Java language is
point for your program.
built because it defines the shape and
nature of an object. Further, applets don’t require a main(
) method at all.
As such, the class forms the basis for
object-oriented programming in A class defines a new type of data (it
Java. is a reference type).
Classes usually consist of two things: In this case, the new data type is
instance variables and methods called Dog.
Any concept you wish to implement You will use this name to declare
in a Java program must be objects of type Dog.
encapsulated within a class.
a class declaration only creates a
A sample of a class is given below: template; it does not create an actual
object.
To actually create a Dog object, use
the second statement.
Dog myDog; // Creates only
the template
myDog = new Dog(); //
create a Dog object called
myDog
After this statement executes,
myDog will be an instance of
The code is contained within Dog.
methods (The operation is done
Thus, it will have “physical”
inside)
reality .
The java final keyword can be used It inherits all of the instance variables
in various context: and methods defined by the superclass
and add its own, unique elements.
1. final variable - A variable
declared as final prevents the The extends keyword: is the keyword
content of that variable being used to inherit the properties of a class.
modified Below given is the syntax of extends
keyword.
2. final method - A method
declared as final prevents the Sytax:
user from overriding that class Super{
method
.....}
3. final class - A class declared
as final cannot be extended class Sub extends Super{ }
thus prevents inheritance
Polymorphism
The dictionary definition of Types of Polymorphism
polymorphism refers to a principle in There are 2 basic types of
biology in which an organism or species polymorphism:
can have many different forms or stages.
1. Static Polymorphism
This principle can also be applied to
2. Dynamic Polymorphism.
object-oriented programming like the
Java language Some programmers classify
polymorphism in to three:
Polymorphism is the ability of an object
to take on many forms. 1. Ad-hoc (overloading and
overriding),
The most common use of polymorphism
in OOP occurs when a parent class 2. Parametric (generics) and
reference is used to refer to a child class
object. 3. Dynamic method binding.
Any Java object that can pass more than 1. Static polymorphism
one IS-A test is considered to be Static Polymorphism is in other words
polymorphic. termed as compile-time binding or early
binding.
In Java, all Java objects are polymorphic
since any object will pass the IS-A test Static binding occurs at compile time.
for their own type and for the class Method overloading is a case of static
Object. binding and in this case binding of
Polymorphism is derived from 2 Greek method call to its definition happens at
words: poly and morphs. the time of compilation.