Chapter 4 - OOP Concepts (4)
Chapter 4 - OOP Concepts (4)
Chapter Four:
Object Oriented Concepts
Objectives:
At the end of this chapter you will be able to understand and practice the following OO concepts
programmatically.
Inheritance
Polymorphism
Encapsulation
Method overloading and overriding
Packages and interfaces
4.1. Encapsulation
Question: What is a class? How is it constructed in Java?
If you answer the above questions, it is easier for you to understand what encapsulation is.
Encapsulation is the concept and it becomes a class programmatically. Encapsulation is a concept of
putting both data and code into a capsule. In other words instance variables and methods are written
inside a container. This container is the class that you already read it in the previous chapter.
Encapsulation gives us advantages. The core concept that it gives us is information hiding. Information
hiding is one of the key features of object oriented programming. It is the concept of restricting/allowing
the resources of an object. An object’s instance variables and methods can be set to be not accessed by
other objects. Only those variables and methods that are set to be accessed by others are available.
4.2. Inheritance
Inheritance allows the hierarchical classification of objects. Using inheritance, you can create a general
class that defines traits common to a set of related items. This class can then be inherited by other, more
specific classes, each adding those things that are unique to it. A class that is inherited is called a
superclass. The class that does the inheriting is called a subclass. A subclass is a specialized version of a
superclass. It inherits all of the members defined by the superclass and adds its own, unique elements.
InjibaraComminity
Staff Student
InjibaraComminity is the superclass and it defines the common data and code that will apply to all the
subclasses, i.e., all the classes down the hierarchy. Staff class inherits the data and code defined by
InjibaraComminity and it adds the data and code that are specific to staffs. The Staff class defines those
things that are common to both admin and academic staff. AdminStaff inherits from Staff directly and
from InjibaraComminity indirectly. The same concept applies to the Student class and its subclasses.
Note:- The level of the hierarchy is not limited. For example, academic staff can further be classified
into faculties; technology, science, business and economics, etc. The level is decided by us. If we
believe this is enough as a solution, we can stop.
In java, multiple inheritance is not allowed. In other words a class cannot have two or more direct
superclasses. AdminStaff has one direct superclass, that is, the Staff class. It has an indirect superclass
which is the InjibaraComminity class. So a class is not allowed only to have on direct superclass but it
can have many indirect superclasses.
The syntax of creating a subclass is as follows:
class SubclassName extends SuperclassName{
//instance variables
//methods
}
The keyword extends is used to define a relationship of inheritance. SubclassName is the name of the
subclass and SuperclassName is the name of the superclass.
Using this syntax, we can create the programmatic equivalent of the above diagram as follows. You
must carefully examine the example.
4.3. Polymorphism
As you may remember polymorphism is the concept using a single interface to represent multiple
different actions. Compile time polymorphism is supported by method overloading. Runtime
polymorphism is realized through method overriding.
Dynamic, runtime polymorphism is one of the most powerful mechanisms that object oriented design
brings to bear on code reuse and robustness.
4.7. Interface
An interface is similar to a class and it is completely abstract. An interface has no instance variables. To
declare an interface, use the keyword interface.
interface Interface_name{
type var1 = value1;
type var2 = value2;
.
type varN = valueN;
type method_name1(parameter-list);
type method_name2(parameter-list);
.
type method_nameN(parameter-list);
}
All the variables of an interface are static and final by default. All the methods of an interface are
abstract by default. Both the methods and variable are also public. The interface itself can be declared
public so that it can be accessed by all part of your program. If public is not specified, the default access
is applied, as a result, the interface can only be accessed inside the package it is declared. Packages are
discussed later in this chapter.
An interface is useless unless it is implemented by a class. A class can implement any number of
interfaces and an interface can be implemented by any number of classes. When we say a class
implements an interface, it is must for the class to provide implementation (body) for each and every
method of the interface. If one of the methods is left unimplemented by the class, the class itself must be
declared as abstract; otherwise it is a compiler error. One more thing to add, all the implemented
methods in the implementer class must be declared as public; otherwise the compiler will issue an error.
class classname [extends superclass] [implements interface1, interface2, ….., interfaceN] {
//class body must provide code for each and every method of the interfaces
}
A reference variable of type interface can be declared. This reference variable can refer to the object of
any of the classes that implement the interface by which a reference variable is created. This is possible
since the interface can be considered as a superclass for those classes that implement the interface
directly or indirectly. On the other hand, it is not allowed to create object of an interface.
If a reference variable of an interface can refer to objects of any of the classes implementing the
interface, using interfaces, you can still realize polymorphism. Polymorphism came to practice through
the use of inheritance, method overriding and upcasting. All these things are also available when
implementing interfaces.
The examples below describe all the concepts discussed here.
interface Example{
int x = 10; The class A provides the correct implementation for all
int add(int a, int b); the methods of the interface so that there is no any
float mult(float y, float z); problem.
}
class A implements Example{ The class B leaves the mult() method unimplemented.
public int add(int i, int j){ As a result a compiler error will appear. To fix this
return i+j; problem, either class B must be declared abstract or
} class B must provide implementation (may be empty
public float mult(float y, float z){ body if it can’t provide or may not need the method at
return y*z; all) for mult() method.
}
} Question:
class B implements Example{ Based on the given explanation, correct the error found
public int add(int c, int d){ in class B. Try both solutions.
return c+d;
}
}
Another example:
interface Example{
int x = 10;
int add(int a, int b);
float mult(float y, float z);
}
class A implements Example{
public int add(int i, int j){
return i+j;
}
public float mult(float y, float z){
return y*z;
}}
class B implements Example{
public int add(int c, int d){
return c+d;
}
public float mult(float m, float n){
return m*n;
}}
class Main{
public static void main(String []argument){
Example e;
//e = new Example(); this is incorrect
e = new A();
System.out.println(e.add(20, 25) + " " + e.mult(2,8));
e = new B();
System.out.println(e.add(20, 25) + " " + e.mult(2,8));
}}
4.8. Package
In the examples used up to this time, the name of each example class was taken from the same name
space. This means that a unique name had to be used for each class to avoid name collisions. After a
while, without some way to manage the name space, you could run out of convenient, descriptive names
for individual classes. You also need some way to be assured that the name you choose for a class will
be reasonably unique and not collide with class name chosen by other programmers. (Imagine a small
group of programmers fighting over who gets to use the name “Foobar” as a class name. Or, imagine the
entire internet community arguing over who first named a class “Espresso”.) Thankfully, java provides a
mechanism for partitioning the class name space into more manageable chunks. This mechanism is the
package.
The package is both a naming and a visibility control mechanism. You can define classes inside a
package that are not accessible by code outside that package. You can also define class members that are
exposed only to other members of the same package. This allows your classes to have intimate
knowledge of each other, but not expose that knowledge to the rest of the world.