Object Oriented Programming
Object Oriented Programming
College of Computing
March 2023
Debre Berhan,
Ethiopia
The Java platform consists of:
Chapter One: Object Oriented the Java VM
Programing
the Java API
1.1. Overview (Application
Object oriented programming: allow the Programming
programmer to break down the problem into Interface).
objects.
The API provides libraries, which
Objects: self-contained entities consisting of groups a number of related classes
both data and operations together. and interfaces together into what is
E.g. java, C++, C#, VB6, Python… called a Java package.
These are local, instance and The word polymorphism means the
Class/static variable ability to take more than one form.
፬. Abstraction: In terms of the OOP, this means that
a particular operation may behave
provides only essential information differently for different sub-classes
to the outside world. of the same class.
Or hiding their background details, Components of java program
i.e., to represent the needed
information in program without Java program = Comments + Token(s) +
presenting the details. white space
For example, a bank system. 1. Comments: Java supports three
types of comment delimiters. A
single line comment //, 2 multiline 3. Whitespace in Java is used to
comments /* …. */ and /** ….*/. separate the tokens in a Java source
The las one is used by javaDoc file. it is required in some places,
2. Token is the smaller individual units such as between access modifiers,
inside a program the compiler type names and Identifiers, and is
recognizes when building up the used to improve readability
program. elsewhere.
Thus, the data for one object is 1. Wrapper Class:- The eight
separate and unique from the data for classes of java.lang package
another.
2. Abstract Class:- A classes 1. Supper Class: The class that
declared with abstract is inherited.
keyword and can’t be
2. Sub Class: The class that
instantiated.
does the inheriting.
3. Final Class:- A class
መ. Based on the source file they are located
declared with final keyword
classes are classified in to two.
and can’t be inherited.
1. Internal classes: Located at
4. Input-Output Class:-
the starting pointing file,
Available in java.io package.
includes main().
contains all the required
classes to perform I/O 2. External classes: Located in
operations a separate source file (outside
the starting pointing file).
5. String Class:- Immutable
Class created to manipulate
strings(Sequence of chars).
2.3. Source file declaration
6. System Class:- Contains rules
several useful class fields and
methods. It cannot be
instantiated. E.g. standard These rules are essential when declaring
input, standard output, and classes, import statements and package
error output streams statements in a source file.
7. Mutable Class:- Have fields 1. There can be only one public
that can be changed. class per source file.
ለ. According to access modifiers: Classes 2. A source file can have multiple
are classified in to 4: nonpublic classes.
1. Public Classes: Accessible 3. The public class name should be
to the world the name of the source file as
well which should be appended
2. Private Class: Used only by .java at the end.
inside a block(e.g with in the
outer class) For example: the class name is public class
Employee{} then the source file should be
3. Protected Class: Used in as Employee.java.
parent class, package and
other subclasses If the class is defined inside a
package, then the package statement
4. Default Class: Used only in should be the first statement in the
parent class and package source file.
ሐ. According to Inheritance: Classes are
If import statements are present then
classified in to 2: they must be written between the
package statement and the class class DogDemo { // This class declares an
declaration. object of type Dog.
public static void main(String args[]) {
If there are no package statements Dog myDog = new Dog();
then the import statement should be String doginfo; //used to hold new Dog’s
the first line in the source file. attribute
myDog.age = 10;
Import and package statements will myDog.breed=“Australian Cattel Dog”;
imply to all the classes present in the myDog.color = “Red”; //assign values to
source file. doginfo= “ Breed: ” + myDog.breed +
It is not possible to declare different “Dog‘s age:” + myDog.age + “Color: ” +
myDog.color;
import and/or package statements to
System.out.println(“The dog’s " + doginfo);
different classes in the source file.
}}
2.4. Instantiating and
initializing class objects Declaring Objects
Every Dog object will contain its When you create a class, you are
own copies of the instance variables creating a new data type.
breed, age, and color.
You can use this type to declare
To access these variables, you will objects of that type.
use the dot (.) operator.
the following is used to declare an
The dot operator links the name of object of type Box:
the object with the name of an
instance variable. Dog myDog = new Dog();
OR
For example, to assign the age
variable of myDog the value 10, you Dog myDog; // declare
would use the following statement: reference/template to object
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.