Object Oriented
• The main idea of OO is to organize a program in a way that mirrors the real world objects
(attributes and behaviors).
• Object oriented program: is a collection of interacting objects.
• Object: is a program construct that
– Contains data
– Performs certain actions ( methods ).
• Objects of the same class have
Classes
• Class: is a new data type (user defined
data type) that describes what the object is
and what it can do.
• Class: data type, plan, template, or a
References and Aliases
• Primitive types:
byte, short, int, long
float, double, char, boolean
• All other types are reference or class types
String greeting = "Howdy";
– greeting is a reference variable
– Reference: is a variable that holds the memory address of a particular object.
Constructors
• Constructor : is A method that
– Allocates memory for the object – Initializes the data fields• Properties
– Same name as the class
– No return type, not even void
– Any number of formal parameters
• Constructors can be:
- default constructors
Creating objects
• Objects created from classes by: - declaring a variable (reference) - using the constructor of the class - using the New statement
• In this case the created object is an instance of a class.
• Array of objects
• Garbage collection • Packages
Main concepts and features of
object oriented programming
• Encapsulation
• Abstraction
• Inheritance
Encapsulation
• Hides the detail of the inner workings of the class
– The implementation is hidden – Often called "information hiding"
• Part of the class is visible
– The necessary controls for the class are left visible – The class interface is made visible
Abstraction
• A process that has the designer ask what
instead of why
– What is it you want to do
with
the data – What will be doneto
the data• The designer does not consider how the
class's methods will accomplish their goals
• The client interface is the what
Abstraction
Inheritance
• Inheritance: is a process of deriving new class from already existing class.
• A general or base class is first defined Then a more specialized class is defined by …
– Adding to details of the base class (data/methods) – Revising details of the more general class (overriding) • Advantages
– Saves work
– Common properties and behaviors are define only once for all classes involved
• In a derived class
– The constructor must call the base class constructor
• Can use the reserved word super as a name for the constructor of the base class
– When super is used, it must be the first action in the derived constructor definition
Inheritance
Object Types of a Derived Class
• Given :
– Class
CollegeStudent
,– Derived from class
Student
• Then a CollegeStudent
object is also a
Student
object
• In general …
Polymorphism
• When one method name in an instruction can cause different actions – Happens according to the kinds of objects that invoke the
methods
– When an overridden method is used The action is for the method defined in the class whose constructor created the object
– An object, not its name, determines its behavior. • Example
The object still remembers it is of type UndergradStudent
UndergradStudent ug = new UndergradStudent(. . .); Student s = ug; // s and ug are aliases
Interface
• A program component that contains
– Public constants
– Signatures for public methods – Comments that describe them
• Begins like a class definition
– Use the word interface instead of class
public interface someClass {
Java Interface Example
public interface NameInterface
{ public void setName(String firstName, String lastName);
public String getName();
public void setFirst(String firstName);
public String getFirst();
public void setLast(String lastName);
public String getLast();
public void giveLastNameTo(NameInterface child);
Software specification
• Software specification : is a detailed description of the operations (functions , methods ), inputs, processing, outputs, and special requirements of a software product.
• Specifying Methods:
– Specify what each method does – Precondition
• Defines responsibility of client code (client/method responsibility)
– Postcondition
• Specifies what will happen if the preconditions are met
– Assertions can be written as comments to identify design logic
Example of methods specifying
1. Client responsibility:
/** task: computes the square root of a number *@ param x a real number>=0
*@ return the square root of x */
2. Method responsibility:
/** task: computes the square root of a number *@ param x a real number
Implementing an Interface
• A class that implements an interface must state so at start of definition
public class myClass implements someInterface
• The class must implement every method declared in the interface
• Multiple classes can implement the same interface
• A class can implement more than one interface • An interface can be used as a data type
Type Casts Within an Interface
Implementation
• In any class that implements an interface
– A parameter may be of type Object– A type cast would probably be needed within that method public class Pet implements Comparable
{ private String name;
private int age; // in years
private double weight; // in pounds
/** Task: Compares the weight of two pets. */
public int compareTo (Object other) { Pet otherPet = (Pet)other;
return weight - otherPet.weight; } // end compareTo
< Other methods are here. >
Composition
• When a class has a data field that is an instance of another class
• Example – an object of type Student.
• A Student object composed of other objects
Inner classes
• An inner class is an example of composition
• An inner class, or nested class, is a class defined within the scope of another class. • Advantages:
- can make programs simple
- an inner class can reference the data and methods of outer class without passing outer class reference
• Example:
public class outerclass{ private int data;
public void m(){ //do some thing
innerclass obj1= new innerclass(); }// end of m()
class innerclass{ public void mi(){
data ++; m();
Aggregation and composition
• Aggregation and composition represent two kinds of containment relationships between instances of the corresponding classes
• Aggregation
– Contained object is part of container, but can exist independently – Ownership of contained object is a policy decision
– Contained object may be part of other aggregations
– Example: In Java, create an object and place it in a container, but maintain an external reference to object:
• ArrayList aList = new ArrayList();
Aggregation and composition
•
Composition
– Contained object does not exist independently of its container
– Contained object is owned by container
– Contained object may be part of only one composition (only one owner)
– Example: In Java, create an anonymous instance and place it directly in a container, with no external
reference to the object:
Aggregation and composition
• Example: Create an object in a class constructor: • /**
• * Sample of composition. • */
• public class MyIntegerClass {
• private Integer value; // Value of instance. • /**
• * Constructor initializing the instance value.
• *
• * @param value value assigned to instance
• */
• public MyIntegerClass( int value ) { • this.value = new Integer( value );
• }