Chapter 1 Overview of OOP
Chapter 1 Overview of OOP
0 OOP
0 Classes and Objects
0 Encapsulation
0 Abstraction
0 Inheritance
0 Polymorphism
0 Abstract class & interfaces
0 Exceptions & Review of Java Fundamentals
3
Chapter 1 - OOP Principles
Object Oriented Programming
0 An object-oriented programming language provides support for
the following object-oriented concepts:
0 Objects
0 Classes
0 Inheritance
0 Polymorphism
0 Abstraction
0 Modularity
0 Encapsulation
4
Chapter 1 - OOP Principles
Object Oriented Programming
Benefits of OOP
0 The concept of inheritance and the data-oriented approach allow a lot of
reuse of existing classes and helps to eliminate redundant code.
0 Programs can be built using working modules that already know how to
communicate with each other. This means that programs do not always have
to be written from scratch – thus saving development time and increasing
productivity.
0 Encapsulation (hiding of data) helps with the building of more secure
programs – as data cannot be unintentionally changed by other parts of the
program.
0 There is a close link between objects in the real-world system and objects in
the program. Therefore the structure of the system is more likely to be
meaningful to users.
0 The work for a project can be divided by class/object – making it easier to
split work up between a team of developers.
0 OO systems can be easily upgraded from small to larger systems.
0 The message passing technique for communication between objects makes it
easy to describe the interface of an OO system for other systems that need to
communicate with it.
0 Software complexity can be easily managed.
5
Chapter 1 - OOP Principles
Class
0 Definition: A class is a blueprint that defines the variables and the
methods common to all objects of a certain kind.
0 A class is a software blueprint for objects. A class is used to
manufacture or create objects.
0 The class declares the instance variables necessary to contain the state
of every object.
0 The class would also declare and provide implementations for the
instance methods necessary to operate on the state of the object.
0 After you’ve created the class, you can create any number of objects
from that class.
0 Parts of the class specify, or describe, what variables and methods the
objects will contain.
0 Objects are created and destroyed as the program runs, and there can
be many objects with the same structure, if they are created using the
same class.
6
Chapter 1 - OOP Principles
Class
0 Declaring a Class
class classname {
datatype instance-variable1;
datatype instance-variable2;
// ...
datatype instance-variableN;
returntype methodname1(parameter-list) {
// body of method
}
returntype methodname2(parameter-list) {
// body of method
}
// ...
returntype methodnameN(parameter-list) {
// body of method
}
}
0 You can also add modifiers like public or private at the very beginning.
7
Chapter 1 - OOP Principles
Class
0 In general, class declarations can include these components, in
order:
0 Modifiers such as public, private, protected, …
0 The class name,
0 with the initial letter capitalized by convention.
0 The name of the class's parent (superclass), if any,
0 preceded by the keyword extends. A class can only extend (subclass) one
parent.
0 A comma-separated list of interfaces implemented by the class, if
any,
0 preceded by the keyword implements. A class can implement more than
one interface.
0 The class body, surrounded by braces, {}.
8
Chapter 1 - OOP Principles
Class
Access Protection
Variables or Methods with
this modifier can be variable or method modifier
same class Y Y Y Y
Classes in same package Y N Y Y
Subclasses Y N Y N
Classes in different
packages Y N N N
9
Chapter 1 - OOP Principles
Class
0 Example on Declaring a Class
class Rectangle {
float height;
float width;
//constructors
public void Circle ()
{ radius = 1.0;}
//methods
public double calcArea()
{
return 3.14* radius * radius;
}
Answer:
No memory is allocated when a class is created. Memory allocation
happens only when the actual instances of a class(the objects) are
created.
52
Chapter 1 - OOP Principles
Encapsulation
Class Person{
private String fullName;
public String setFullName(String _fullName){
fullName = _fullName;
}
public String getFullName(){
return fullName;
}
}
So, the use of mutators and accessors provides many advantages. By
hiding the implementation of our Person class, we can make
changes to the Person class without the worry that we are going to
break other code that is using and calling the Person class for
information.
This type of data protection and implementation protection is
called Encapsulation. 53
Chapter 1 - OOP Principles
The Garbage Collector
0 Some object-oriented languages require that you keep track of all the
objects you create and that you explicitly destroy them when they are
no longer needed.
0 Managing memory explicitly is tedious and error-prone.
0 The Java platform allows you to create as many objects as you want
(limited, of course, by what your system can handle), and you don't
have to worry about destroying them.
0 The Java runtime environment deletes objects when it
determines that they are no longer being used. This process is
called garbage collection.
0 As you can see, the subclass B includes all of the members of its
superclass, A. This is why subOb can access i and j and call showij( ).
Also, inside sum( ), i and j can be referred to directly, as if they were
part of B.
0 Even though A is a superclass for B, it is also a completely
independent, stand-alone class. Being a superclass for a subclass does
not mean that the superclass cannot be used by itself. Further, a subclass
can be a superclass for another subclass.
0 You can only specify one superclass for any subclass that you create. Java
does not support the inheritance of multiple superclasses into a single
subclass. (This differs from C++, in which you can inherit multiple base
classes.) You can, as stated, create a hierarchy of inheritance in which a
subclass becomes a superclass of1 another
Chapter subclass. However, no class
- OOP Principles 77 can
be a superclass of itself.
1. Inheritance(cont)
0 Although a subclass includes all of
// A's j is not accessible here.
the members of its superclass, it
class B extends A {
cannot access those members of
int total;
the superclass that have been
void sum() {
declared as private. For example,
total = i + j;
consider the following simple
// ERROR, j is not accessible here
class hierarchy:
}
Example : }
// Create a superclass. class Access {
class A { public static void main(String args[]){
int i; // public by default B subOb = new B();
subOb.setij(10, 12);
private int j; // private to A
subOb.sum();
void setij(int x, int y) { System.out.println("Total is " +
i = x; subOb.total);
j = y; }
}
} Chapter 1 - OOP Principles 78
}
1. Inheritance(cont)
0 This program will not compile because the reference to j inside
the sum( ) method of B causes an access violation. Since j is
declared as private, it is only accessible by other members of its
own class. Subclasses have no access to it.
0 A class member that has been declared as private will remain
private to its class. It is not accessible by any code outside its
class, including subclasses.
99
2. Polymorphism(cont)
Method Overriding(cont)
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}
0 As the comments imply, it is illegal for B to inherit A since A
is declared as final.
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
39 char[] toCharArray()
Converts this string to a new character array.
40 String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default
locale. Chapter 1 - OOP Principles 145
SN
Java – String(cont)
Methods with Description
41 String toLowerCase(Locale locale)
Converts all of the characters in this String to lower case using the rules of the given
Locale.
42 String toString()
This object (which is already a string!) is itself returned.
43 String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default
locale.
44 String toUpperCase(Locale locale)
Converts all of the characters in this String to upper case using the rules of the given
Locale.
45 String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
46 static String valueOf(primitive data type x)
Returns the string representation of the passed data type argument.
}
Java – String(cont)
Java String compare to determine Equality(cont)
0 Comparing using the compareTo Method
0 The compareTo method is used when we need to determine the
order of Strings lexicographically. It compares char values similar
to the equals method. The compareTo method returns a negative
integer if the first String object precedes the second string.
It returns zero if the 2 strings being compared are equal.
It returns a positive integer if the first String object follows the
second string. The following Program would print “name2 follows
name1” In the first case and “name1 follows name3” in the second
case.
0 Example: