Introduction To Object Oriented Programming
Introduction To Object Oriented Programming
Introduction To Object Oriented Programming
CHAPTER ONE
Introduction to Object Oriented Programming
The programming paradigms which are most widely used and implemented
by various programming languages are:
Imperative programming
Object-oriented programming (OOP)
Functional programming (FP)
Generic programming (GP)
Meta-programming (MP)
1. Imperative Programming
The text boxes and buttons with which you are familiar are programming objects
Objects have both data and methods
Objects of the same class have the same data elements and methods
Objects send and receive messages to invoke actions
The real world can be accurately described as a collection of objects that interact
For example, suppose you have a method display( ) that takes no arguments
and returns a value of type int.
Then, if you have an object called obj for which display( ) can be called, you
can say this: int x = obj.display();
The type of the return value must be compatible with the type of x.
This act of calling a method is commonly referred to as sending a message to
an object.
In the preceding example, the message is display() and the object is obj.
Object oriented programming is often summarized as simply “sending
messages to objects.”
To understand how those work, you must first understand arguments and
return values. A class can contain any of the following variable types:
The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
.
Friday, February 16, 2024 Object Oriented Programming in Java 36
2.4. Instance Fields
Instance variables: Instance variables are variables within a class but outside any
method. These variables are initialized when the class is instantiated.
Instance variables can be accessed from inside any method, constructor or blocks
of that particular class.
Class variables: Class variables are variables declared within a class, outside any
method, with the static keyword
Activity 2.2
What are the methods used to print result to console?
What are messages?
How to destroy objects?
.
Friday, February 16, 2024 Object Oriented Programming in Java 37
2.5. Constructors and Methods
A constructor initializes an object when it is created.
It has the same name as its class and is syntactically similar to a method.
However, constructors have no explicit return type.
All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member
variables to zero.
However, once you define your own constructor, the default constructor is
no longer used. A class can have more than one constructor.
If you want to have only a single piece of storage for a particular field or
if you need a method that isn’t associated with any particular object of this class. You
can achieve both of these effects with the static keyword.
When you say something is static, it means that particular field or method is not tied
to any particular object instance of that class.
To make a field or method static, you simply place the keyword before the definition.
For example, the following produces a static field and initializes it:
Types of Inheritance
Single Inheritance: a subclass derives from a single super-class
A very important fact to remember is that Java does not support multiple
inheritance. Therefore following example is illegal:
However, a class can implement one or more interfaces, which has helped
Java get rid of the impossibility of multiple inheritance.
Since every class is a subclass of Object, a variable of type Object can refer
to any object whatsoever, of any type.
Java has several standard data structures that are designed to hold Objects,
but since every object is an instance of class Object, these data structures
can actually hold any object whatsoever.
An abstract class is one that is not used to construct objects, but only as a basis for
making subclasses.
An abstract class exists only to express the common properties of all its subclasses.
The interface keyword produces a completely abstract class, one that provides no
implementation at all.
It allows the creator to determine method names, argument lists, and return
types, but no method bodies. An interface provides only a form, but no
implementation.
However, an interface is more than just an abstract class taken to the extreme, since
it allows you to perform a variation of "multiple inheritance" by creating a class
that can be up cast to more than one base type.
If you leave off the public keyword, you get package access, so the interface is only
usable within the same package. An interface can also contain fields, but these are
implicitly static and final.
To make a class that conforms to a particular interface, use the implements keyword,
which says, "The interface is what it looks like, but now I’m going to say how it
works." Other than that, it looks like inheritance.
So when you implement an interface, the methods from the interface must be
defined as public.
Otherwise, they would default to package access, and you are reducing the
accessibility of a method during inheritance, which is not allowed by the Java
compiler.
Note that every method in the interface is strictly a declaration, which is the only
thing the compiler allows
int QUANTITY = 5; // static & final// Cannot have method definitions:void setColor(); //
Automatically public
System.out.println("Blue"); }
Activity 3.3
Explain polymorphism?
What are abstract classes?
Explain interface and its uses?
The code which is prone to exceptions is placed in the try block. When an
exception occurs, that exception occurred is handled by catch block associated
with it.
Every try block should be immediately followed either by a catch block or finally
block.
A catch statement involves declaring the type of exception you are trying to
catch. If an exception occurs in protected code, the catch block (or blocks) that
follows the try is checked.
Friday, February 16, 2024 Object Oriented Programming in Java 97
4.2. Catching Exceptions …
If the type of exception that occurred is listed in a catch block, the exception is passed to the
catch block.
Example:
The following is an array declared with 2 elements. Then the code tries to access the 3 of
element of the array which throws an exception.
import java .io.*;
public class ExcepTest {
public static void main (String args []) {
try {
int a [] = new int[2];
System.out.println(“Access element three :" + a [3]); }
catch (ArrayIndexOutOfBoundsException e ){
System.out.println("Exception thrown :" + e ); }
System.out.println("Out of the block");
}
}
Apart from the built in exceptions Java provides, exception classes can be
written by the programmer. These can then be thrown like the built in
Java exceptions. Keep the following points in mind when writing
yourown exception classes:
Friday, February 16, 2024 Object Oriented Programming in Java 103
4.5. Declaring Exceptions
All exceptions must be a child of Throwable.
If you want to write a checked exception that is automatically
enforced by the Handle or Declare Rule, you need to extend the
Exception class.
If you want to write a runtime exception, you need to extend the
RuntimeException class.
We can define our own Exception class as below:
class MyException extends Exception {
//for checked exceptions
}
modifier MyException extends RuntimeException {
//for runtime exceptions
}
Activity 4.4
A class named Boss is added to the payroll package that already contains
Employee. The Boss can then refer to the Employee class without using
the payroll prefix, as demonstrated by the following Boss class.
package payroll;
public class Boss {
public void payEmployee (Employee e )
{
e.mailCheck(); }
}
include a package statement along with that name at the top of every
source file that contains the classes, interfaces, enumerations, and
annotation types that you want to include in the package.
The package statement should be the first line in the source file (except
for comments and white space, of course).
There can be only one package statement in each source file, and it
applies to all types in the file.
package animals;
interface Animal{
Now a package/
folder with the name animals will be created in the current directory and
the class files will be placed in it.
Friday, February 16, 2024 Object Oriented Programming in Java 126
5.6. Package Scope
The default access has no keyword, but it is commonly referred to
as package access.
It means that all the other classes in the current package have
access to that member, but to all the classes outside of this
package, the member appears to be private.