This document provides an introduction to object-oriented programming concepts like classes, objects, inheritance, polymorphism and abstraction. It discusses how to define classes and interact with objects, and includes examples of writing classes to represent real-world entities like departments and employees.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views27 pages
Lecture 2
This document provides an introduction to object-oriented programming concepts like classes, objects, inheritance, polymorphism and abstraction. It discusses how to define classes and interact with objects, and includes examples of writing classes to represent real-world entities like departments and employees.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27
Introduction to
Object Oriented Programming
ITEC-2610 B : Fall 2023
Khairul bashar Implementing Classes • ITEC1620 was focused on writing applications using objects described by APIs, or by existent classes. • It had little emphasis on writing blueprint-type classes. When the blueprint-type classes were given the 1620 programmer had to write only the driver class. Classes • All Java code must be inside classes • Not all classes are “blueprints” to build objects • Class types • Driver classes • Contains only the method main() • Abstract Data Types (ADTs) • “Blueprints” to build objects • Utility classes, such as Math class • Used to store constants and utility methods Interacting with Objects Object: an entity in your program that you can manipulate by calling one or more of its methods. Method: consists of a sequence of instructions that can access the data of an object. (You do not need to know how a method was written to be able to use it ) “Blueprint” Classes A class describes a set of objects with the same behavior
A class is like a “blueprint” for making objects.
Example:
• There may be thousands of other cars of the same
make and model. • Each car was built from the same set of blueprints and therefore contains the same components. • In object-oriented terms, we say that a car is an instance of the class of objects known as cars. • A class is the blueprint from which individual objects are created. Using APIs in a Simple Example class Department API a) Develop the Java application App whose main String deptName; method performs the following tasks, in the order double budget; shown: 1. Create a department called "R&D" with budget int number; // of employees 2,000,000. // constructor 2. Create an employee John whose rank is 3. Department(String n, double b); 3. Create an employee Debbie whose rank is 2. 4. Assign both John and Debbie to the R&D department 5. Determine the head count of the R&D department void assign(Employee who); and store it in the variable count. int getHeadCount(); 6. If count is greater than 10, increase the department double getBudget(); budget by 5%, otherwise reduce it by 2% 7. Display the departmental budget void changeBudget(double delta) /* delta - change the budget by adding this amount to it b) Write the content of the ADT classes Department and (to reduce budget, provide a negative amount) */ Employee.
class Employee API
String empName; int empRank; // constructor Employee(String name, int rank); Solution – The Driver class public class App { public static void main (String[] args) {
Department d = new Department("R&D", 2000000);
Employee e1 = new Employee("John", 3); Employee e2 = new Employee("Debbie", 2);
d.assign(e1); // using a public method of class Department
d.assign(e2); int nr = d.getHeadCount();
if (nr>10) d.changeBudget(d.getBudget()*0.05); else d.changeBudget(-d.getBudget()*0.02);
System.out.println("The budget is = "+ d.getBudget());
} } Solution – ADT classes class Department { class Employee { private String deptName; private double deptBudget; private int number; private String empName; private int empRank; public Department(String name, double budget) { deptName=name; public Employee(String name, int rank){ deptBudget=budget; empName=name; number=0; } empRank=rank; } public void assign (Employee who) { number++; } }
public double getBudget(){ An object of Employee type
return deptBudget; empName }
public void changeBudget(double delta){
empRank deptBudget += delta; }
public int getHeadCount(){
return number; } } Writing ADT classes • class attributes should be encapsulated (you must use private when you declare them) • class methods are normally public • constructors are used to enter the initial data into the encapsulated attributes • provide methods to change that data • In the example shown before only the methods needed by the application were provided • In general ADT classes have all possible methods to change the encapsulated data Writing ADT classes (II) • The ADT class may contain the method main() if the driver deals with a single ADT class. Alternatively, you can write a separate Driver class, which contains only the method main(). • If the driver uses more than one type of object the method main() must be inside a Driver class. A More Detailed Discussion • The next example presents class BankAccount, which will be used to discuss some important features: • Overloaded constructors • Scope of variables • Parameters passing • Static variables, constants and methods Inheritance
• Pillars of Object Oriented Programming
• Data encapsulation • Inheritance • Polymorphism • Abstraction • Motivation for Inheritance • Code reuse • Conceptual modeling Inheritance
• Base class, derived class
• Rules when deriving subclasses • Important reserved words: protected, super • Multiple inheritance • Overloading vs overriding Inheritance (II) • Inheritance allows a software developer to derive a new class from an existing one • The existing class is called the parent class, or superclass, or base class • The derived class is called the child class or subclass • As the name implies, the child inherits characteristics of the parent • That is, the child class inherits the methods and data defined by the parent class Inheritance class diagram • 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
• Proper inheritance creates an is-a relationship,
meaning the child is a more specific version of the parent Benefits of Inheritance
• A programmer can tailor a derived class as needed by
adding new variables or methods, or by modifying the inherited ones • One benefit of inheritance is software reuse • By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software Deriving Subclasses • In Java, we use the reserved word extends to establish an inheritance relationship • The next example illustrates the use of classes Book and Dictionary, which are in an inheritance relationship. • The next slide shows a UML diagram with these classes. The arrow points to the base class Book. UML diagrams are not going to be examined in this course. The protected Modifier
• Visibility modifiers affect the way that class
members can be used in a child class • Variables and methods declared with private visibility cannot be referenced in a child class • They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation • There is a third visibility modifier that helps in inheritance situations: protected The protected Modifier (II) • The protected modifier allows a child class to reference a variable or method in the child class • It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility • A protected variable is also visible to any class in the same package as the parent class (but we do not deal with packages in ITEC2610) • Protected variables and methods can be shown with a # symbol preceding them in UML diagrams The super Reference • Constructors are not inherited, even though they have public visibility • Yet we often want to use the parent's constructor to set up the "parent's part" of the object • The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor • A child’s constructor is responsible for calling the parent’s constructor Multiple Inheritance
• Java supports single inheritance, meaning that a derived
class can have only one parent class • Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents • Collisions, such as the same variable name in two parents, have to be resolved • Multiple inheritance is achieved in Java by using interfaces (see next lecture). Overriding Methods • A child class can override the definition of an inherited method in favor of its own • The new method must have the same signature as the parent's method, but can have a different body • The type of the object executing the method determines which version of the method is invoked Overriding
• A method in the parent class can be invoked explicitly
using the super reference • If a method is declared with the final modifier, it cannot be overridden • The concept of overriding can be applied to data and is called shadowing variables • Shadowing variables should be avoided because it tends to cause unnecessarily confusing code Abstraction • Abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). • Can also declare classes with no abstract methods as abstract • preventing users from creating instances of that class • Why use abstract classes? • Forcing programmers to create subclasses • Avoiding useless default methods in superclasses others may inherit by accident. • Force subclasses to implement required methods Polymorphism • Polymorphism describes the ability to something different ways. • It also known as method overriding. • It increases re-usability of the code • It reduces duplicate code • Polymorphism is used to implement inheritance Encapsulation • Encapsulation refers to bundling of data along with the methods that operate on that data. • It restricts directs access to the components as desired • Advantages: • Data hiding • Easy to understand • Flexibility Overloading vs. Overriding
• 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