1a - CSC584 - Overview of OOP - Part 1
1a - CSC584 - Overview of OOP - Part 1
REVIEW OF
OBJECT ORIENTED
PROGRAMMING
CONCEPTS
7
OBJECTS,
CLASSES &
PACKAGES
OO PROGRAMMING CONCEPTS
▪ Object-oriented programming (OOP) involves programming using
objects.
▪ An object represents an entity in the real world that can be
distinctly identified.
▪ For example, a student, a desk, a circle, a button, and even a loan
can all be viewed as objects.
▪ An object has a unique identity, state, and behaviors.
OO PROGRAMMING CONCEPTS
▪ The state of an object consists of a set of data fields (also known
as properties) with their current values.
▪ The behavior of an object is defined by a set of methods.
OBJECTS
An object has both a state and behavior. The state defines the
object, and the behavior defines what the object does.
CLASSES
▪ Classes are constructs that define objects of the same type.
▪ A Java class uses variables to define data fields and
methods to define behaviors.
▪ A class provides a special type of methods, known as
constructors, which are invoked to construct objects from
the class.
CLASSES
UML CLASS DIAGRAM
CONSTRUCTORS
Circle() { Constructors are a special kind of methods
} that are invoked to construct objects.
Circle(double newRadius) {
radius = newRadius;
}
CONSTRUCTORS, CONT.
▪ A constructor with no parameters is referred to as a no-arg
constructor.
▪ Constructors must have the same name as the class itself.
▪ Constructors do not have a return type—not even void.
▪ Constructors are invoked using the new operator when an object is
created. Constructors play the role of initializing objects.
CREATING OBJECTS USING
CONSTRUCTORS
new ClassName();
Example:
new Circle();
new Circle(5.0);
DEFAULT CONSTRUCTOR
▪ A class may be declared without constructors.
▪ A no-arg constructor with an empty body is implicitly
declared in the class.
▪ This constructor, called a default constructor, is provided
automatically only if no constructors are explicitly
declared in the class.
DECLARING OBJECT REFERENCE VARIABLES
ClassName objectRefVar;
Example:
Circle myCircle;
DECLARING/CREATING OBJECTS
IN A SINGLE STEP
ClassName objectRefVar = new ClassName();
Verbs
INHERITANCE & POLYMORPHISM
CONCEPT
INHERITANCE
▪ A process in which a new class is derived from an existing one.
▪ Parent class/super class: existing class.
▪ Child class/sub class: derived class.
▪ Child inherits characteristics of the parent - methods and data defined by the
parent class.
▪ Can tailor a derived class as needed by adding new variables or methods, or by
modifying the inherited ones.
INHERITANCE
▪ Inheritance relationships are shown in a UML class diagram using a solid arrow
with an unfilled triangular arrowhead pointing to the parent class
Vehicle
Car
Rabbit Cat
THE PET CLASS
class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String petName) {
name = petName;
}
public String speak( ) {
return “I’m your cuddly little pet.”;
}
}
SUBCLASSES OF THE PET CLASS
class Cat extends Pet { The Cat subclass overrides
public String speak( ) { the inherited method speak.
return “Don’t give me orders.\n” +
“I speak only when I want to.”;
}
}
▪ Case Study:
▪ Suppose we want to implement a class roster that contains both
undergraduate and graduate students.
▪ Each student’s record will contain his or her name, three test scores, and the
final course grade.
▪ The formula for determining the course grade is different for graduate
students than for undergraduate students.
MODELING TWO TYPES OF
STUDENTS
▪ There are two ways to design the classes to model undergraduate and graduate
students.
▪ We can define two unrelated classes, one for undergraduates and one for
graduates.
▪ We can model the two kinds of students by using classes that are related in
an inheritance hierarchy.
▪ Two classes are unrelated if they are not connected in an inheritance
relationship.
CLASSES FOR THE CLASS ROSTER
▪ For the Class Roster sample, we design three classes:
▪ Student
▪ UndergraduateStudent
▪ GraduateStudent
▪ The Student class will incorporate behavior and data common to both
UndergraduateStudent and GraduateStudent objects.
▪ The UndergraduateStudent class and the GraduateStudent class will each
contain behaviors and data specific to their respective objects.
CLASSES FOR THE CLASS ROSTER -
CREATING THE ROSTER ARRAY
▪ We can maintain our class roster using an array, combining objects from the
Student, UndergraduateStudent, and GraduateStudent classes.
▪ If your method overrides one of its superclass's methods, you can invoke the
overridden method through the use of the keyword super.
▪ You can also use super to refer to a hidden field.
EXAMPLE: USING SUPER KEYWORD
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
▪ Overloading deals with multiple methods with the same name in the same class,
but with different signatures
▪ Overriding deals with two methods, one in a parent class and one in a child
class, that have the same signature
▪ Overloading lets you define a similar operation in different ways for different
parameters
▪ Overriding lets you define a similar operation in different ways for different
object types
EXAMPLE: METHOD OVERLOADING
class Sample{
public static void main(String args[]){
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
class DisplayOverloading{
public void disp(char c){
System.out.println(c);
}
public void disp(char c, int num) {
System.out.println(c + " "+num);
}
}
CLASS HIERARCHIES
▪ A child class of one parent can be the parent of another child, forming a class
hierarchy.
CLASS HIERARCHIES
▪ Two children of the same parent are called siblings.
▪ Common features should be put as high in the hierarchy as is reasonable.
▪ An inherited member is passed continually down the line.
▪ Therefore, a child class inherits from all its ancestor classes.
POLYMORPHISM
▪ Polymorphism allows a single variable to refer to objects from different
subclasses in the same inheritance hierarchy
▪ For example, if Cat and Rabbit are subclasses of Pet, then the following
statements are valid:
Pet myPet;
Pet
class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);//true
}
}
THE INSTANCEOF OPERATOR
▪ An object of subclass type is also a type of parent class.
▪ For example, if Cat extends Animal then object of Cat can be referred by either
Cat or Animal class.
class Animal{}
class Cat1 extends Animal{//Cat inherits Animal
public static void main(String args[]){
Cat1 c=new Cat1();
System.out.println(c instanceof Animal);//true
}
}
THE INSTANCEOF OPERATOR
▪ The following code counts the number of undergraduate students.
int undergradCount = 0;
for (int i = 0; i < numberOfStudents; i++) {
if ( roster[i] instanceof UndergraduateStudent ) {
undergradCount++;
}
}
INSTANCEOF: EXAMPLE
▪ Suppose you have four classes which are Animal, Animal
Mammal, Reptile, and Dolphin as shown. This can be
translated into the following:
Now, based on the above example, In Object Oriented terms, the following are true:
· Animal is the superclass of Mammal class.
· Animal is the superclass of Reptile class.
· Mammal and Reptile are subclasses of Animal class.
· Dolphin is the subclass of both Mammal and Animal classes.
INSTANCEOF: EXAMPLE CONT…
▪ Now, if we consider the IS-A relationship, we can say:
· Mammal IS-A Animal
· Reptile IS-A Animal
· Dolphin IS-A Mammal
· Hence : Dolphin IS-A Animal as well
▪ With use of the extends keyword the subclasses will be able to inherit all the
properties of the superclass except for the private properties of the superclass.
INSTANCEOF: EXAMPLE CONT…
public class Test {
Instances
Class Hierarchy
THE EFFECT OF THREE VISIBILITY
MODIFIERS package one
package two
ACCESSIBILITY OF SUPER FROM SUB
▪ Everything except the private members of the Super class is visible from a
method of the Sub class.
ACCESSIBILITY FROM ANOTHER INSTANCE
▪ Data members accessible from an instance are also accessible from other
instances of the same class.