71 | P a g e
X. Object Oriented Concepts
Objectives
In this chapter we will be introducing to different basic concepts in java. The
inheritance, the polymorphism and lastly interface.
At the end of the lesson, the student should be able to:
• Define super classes and subclasses
• Override methods of superclasses
• Create final methods and final classes
Object‐Oriented Design and Concepts
Object‐oriented design is a technique that focuses design on objects and classes
based on real world scenarios. It emphasizes state, behavior and interaction of objects.
It provides the benefits of faster development, increased quality, easier maintenance,
enhanced modifiability and increase software reuse.
The following are basic concepts in Java:
Class ‐ A class allows you to define new data types. It serves as a blueprint, which
is a model for the objects you create based on this new data type.
Object ‐ An object is an entity that has a state, behavior and identity with a well‐
defined role in problem space. It is an actual instance of a class. Thus, it is also
known as an instance. It is created every time you instantiate a class using the
new keyword.
o Attribute ‐ An attribute refers to the data element of an object. It stores
information about the object. It is also known as a data member, an
instance variable, a property or a data field. Going back to the student
registration system example, some attributes of a student entity include
name, student number and school level.
Method ‐ A method describes the behavior of an object. It is also called a
function or a procedure.
o Constructor ‐ A constructor is a special type of method used for creating
and initializing a new object. Remember that constructors are not
members (i.e., attributes, methods or inner classes of an object).
Package ‐ A package refers to a grouping of classes and/or sub packages. Its
structure is analogous to that of a directory.
Encapsulation ‐ Encapsulation refers to the principle of hiding design or
implementation information that is not relevant to the current object.
Abstraction ‐ While encapsulation is hiding the details away, abstraction refers
to ignoring aspects of a subject that are not relevant to the current purpose in
order to concentrate more fully on those that are.
Training-workshop on Object-oriented Programming using Java
72 | P a g e
Polymorphism ‐ Polymorphism is the ability of an object to assume may
different forms. Literally, "poly" means many while "morph" means form.
Interface ‐ An interface is a contract in the form of a collection of method and
constant declarations. When a class implements an interface, it promises to
implement all of the methods declared in that interface.
Inheritance
In Java, inheritance refers to a feature of object‐oriented programming that lets you
create classes that are derived from other classes. A class that’s based on another class
is said to inherit the other class.
Inheritance hierarchies
One of the most important aspects of inheritance is that a class that’s derived from a
base class can in turn be used as the base class for another derived class. Thus, you can
use inheritance to form a hierarchy of classes.
Figure 10.0: Class hierarchy in Java
As we create a subclass, it automatically given all the methods and fields defined by its
superclass. A derived class can add features to the base class it inherits by defining its
own methods and fields. This is one way a derived class distinguishes itself from its base
class.
Superclasses and Subclasses
The class that is inherited is called the parent class, the base class, or the superclass. The
class that does the inheriting is called the child class, the derived class, or the subclass.
Here is a parent class named Personnel:
public class Personnel {
protected String name;
protected int age;
Training-workshop on Object-oriented Programming using Java
73 | P a g e
public Personnel (){
System.out.println(“Inside Personnel:Constructor”);
name = "";
age = "";
}
public Person( String name, int age ){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void setName( String name ){
this.name = name;
}
public void setAge( int age ){
this.address = age;
}
}
Creating Subclasses
To create a child class or a subclass based on an existing class, we use the extends
keyword in declaring the class. A class can only extend one parent class.
Syntax of class declaration for class that inherits superclass:
public class ClassName extends BaseClass
{
// class body goes here
}
For example:
public class Employee extends Personnel {
System.out.println(“Inside Employee:Constructor”);}
}
Here, since the Employee is category of Personnel we just extends the class Employee to
Personnel so that we can inherit all the properties and methods of the existing class
Personnel.
When a certain object is instantiated, the default constructor of its superclass is invoked
implicitly to do the necessary initializations. After that, the statements inside the
subclass are executed.
public static void main( String[] args )
{
Personnel employee = new Personel();
}
Training-workshop on Object-oriented Programming using Java
74 | P a g e
The super keyword
A subclass can also explicitly call a constructor of its immediate superclass using the
super constructor call. It used to invoke superclass constructors. It can also be used like
the this keyword to refer to members of the superclass.
Example:
public Employee(){
super();
System.out.println("Inside Employee:Constructor");
}
In here the super() refers to the immediate superclass. It should be the first statement in
the subclass's constructor.
There are a few things to remember when using the super constructor call:
1. The super() call MUST OCCUR THE FIRST STATEMENT IN A CONSTRUCTOR.
2. The super() call can only be used in a constructor definition.
3. This implies that the this() construct and the super() calls CANNOT BOTH OCCUR IN
THE SAME CONSTRUCTOR.
Overriding Methods
If for some reason a derived class needs to have a different implementation of a certain
method from that of the superclass, overriding methods could prove to be very useful.
A subclass can override a method defined in its superclass by providing a new
implementation for that method.
Suppose we have the following implementation for the getName method in the Person
superclass,
public class Personnel
{
::
public int getAge(){
System.out.println("this is Parent getAge method ");
return age;
}:
}
To override, the getAge method in the subclass employee, we write,
public class Employee extends Personnel
{
::
Training-workshop on Object-oriented Programming using Java
75 | P a g e
public int getAge(){
System.out.println("this is Subclass getAge method”);
return age;
}:
}
When we invoke the getName method of an object of class Employee, the overridden
method would be called, and the output would be,
this is Subclass getAge method
Remember that subclass inherits all the members from its base class. However,
constructors are not considered to be members. As a result, a subclass does not inherit
constructors from its base class.
The visibility (public or private) of any members inherited from the base class is the
same in the subclass. That means that you can’t access methods or fields that are
declared in the base class as private from sthe subclass.
A special type of visibility is called protected that hides fields and methods from other
classes but makes them available to subclasses.
Final Methods and Classes
The other two uses of the final keyword are to create final methods and final classes
Final methods
A final method is a method that can’t be overridden by a subclass. To create a
final method, you simply add the keyword final to the method declaration.
For example:
public class Consultant{
public final String getAddress()
{
return this.address;
}
}
Here, the method getAddress is declared as final. Thus, any class that uses the
Consultant class as a base class can’t override the getAddress method. If it tries, the
compiler issues an error message.
Final methods execute more efficiently than non‐final methods. That’s because the
compiler knows at compile time that a call to a final method won’t be overridden by
some other method. The performance gain isn’t huge, but for applications where
Training-workshop on Object-oriented Programming using Java
76 | P a g e
performance is crucial, it can be noticeable. Private methods are automatically
considered to be final. That’s because you can’t override a method you can’t see.
Final classes
A final class is a class that can’t be used as a base class. To declare a class as final, just
add the final keyword to the class declaration:
public final class Personnel
{
// members for the Personnel class go here
}
Then, no one can use the Personnel class as the base class for another class. When you
declare a class to be final, all of its methods are considered to be final too. Thus, all the
methods of a final class are final methods.
Polymorphism
The term polymorphism refers to the ability of Java to use base class variables to refer to
subclass objects, keep track of which subclass an object belongs to, and use overridden
methods of the subclass even though the subclass isn’t known when the program is
compiled.
we can create a reference that is of type superclass to an object of its subclass.
public static main( String[] args )
{
Personnnel reference;
Employee employeeObject = new Employee();
Consultant consultant Object = new Consultant();
reference = studentObject; //Person reference points to
// Employee object
//some code here
}
Now suppose we have a getName method in our superclass Personnel and we override
this method in both the Consultant and Employee subclasses,
public class Personnel
{
public String getName(){
System.out.println(“Personnel Name:” + name);
return name;
}
}
public class Employee extends Personnel
Training-workshop on Object-oriented Programming using Java
77 | P a g e
{
public String getName(){
System.out.println(“Employee Name:” + name);
return name;
}
}
public class Consultant extends Personnel
{
public String getName(){
System.out.println(“Consultant Name:” + name);
return name;
}
}
Late binding has something to do with which method is to be called because when the
compiler can’t tell for sure what type of object a variable references, it doesn’t hard‐
wire the method calls when the program is compiled. Instead, it waits until the program
is executing to determine exactly which method to call.
Abstraction
An abstract method is just a prototype for a method: a return type, a name, a list of
parameters, and (optionally) a throws clause.
To create an abstract method, you specify the modifier abstract and replace the method
body with a semicolon:
public abstract Employee( );
A class that contains at least one abstract method is called an abstract class, and must
be declared with the abstract modifier on the class declaration.
For example:
public abstract class Personnel{
public abstract Employee();
}
If you omit the abstract modifier from the class declaration, the Java compiler coughs up
an error message to remind you that the class must be declared abstract.
Interfaces
An interface is somewhat similar to an abstract class. Except that it can only include
abstract methods and final fields (constants), and it can’t be used as a base class.
Instead, a class implements an interface by providing an implementation for each
method declared by the interface.
Training-workshop on Object-oriented Programming using Java
78 | P a g e
Interfaces are easier to work with than inheritance, don’t provide any implementation
details in the interface. A class can extend only one other class, but it can implement as
many interfaces as you desired.
Training-workshop on Object-oriented Programming using Java