Review of Object-Oriented Concepts in JAVA

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Object-Oriented PHP (1)
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
ICS 202: Data Structures Fall (081) Section 01: 10-10:50am, 24:116 Section 02: 9 - 9:50am, 24:174 Instructor: Dr. Wasfi G. Al-Khatib Office:
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance ndex.html ndex.htmland “Java.
درس مهندسی اینترنت – مهدی عمادی مهندسی اینترنت برنامه‌نویسی در اینترنت Object Oriented Programming Concepts in JAVA.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
CS102 Algorithims and Programming II1 Inheritance Inheritance defines a relationship among classes. A class C2 may inherit from (extend – derive from)
Data Structures and Algorithms in JAVA Chapter 2.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
Inheritance ITI1121 Nour El Kadri.
7. Inheritance and Polymorphism
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Introduction to Programming with Java
Polymorphism.
Week 4 Object-Oriented Programming (1): Inheritance
Object Oriented Analysis and Design
CSC 113 Tutorial QUIZ I.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Inheritance Inheritance is a fundamental Object Oriented concept
Review of Object-Oriented Concepts in JAVA
Review of Previous Lesson
Chapter 9 Polymorphism.
Object-Oriented PHP (1)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
ITE “A” GROUP 2 ENCAPSULATION.
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes. Interfaces. Review Questions.

Object-Oriented Concepts supported by JAVA Java provides explicit support for many of the fundamental Object-Oriented Concepts. Some of these are: Classification: Grouping related things together. This is supported through classes, inheritance & packages. Encapsulation: Representing data and the set of operations on the data as a single entity - exactly what classes do. Information Hiding: An object should be in full control of its data, granting specific access only to whom it wishes. Inheritance: Java allows related classes to be organized in a hierarchical manner using the extends keyword. Polymorphism: Same code behaves differently at different times during execution. This is due to dynamic binding.

Advantages of Object-Orientation. A number of advantages can be derived as a result of these object-oriented features. Some of these are: Reusability: Rather than endlessly rewriting same piece of code, we write it once and use it or inherit it as needed. Extensibility: A class can be extended without affecting its users provided the user-interface remains the same. Maintainability: Again, once the user-interface does not changed, the implementation can be changed at will. Security: Thanks to information hiding, a user can only access the information he has been allowed to access. Abstraction: Classification and Encapsulation allow portrayal of real-world problems in a simplified model.

Review of inheritance Suppose we have the following Employee class: class Employee { protected String name; protected double payRate; public Employee(String name, double payRate) { this.name = name; this.payRate = payRate; } public String getName() {return name;} public void setPayRate(double newRate) { payRate = newRate; public double pay() {return payRate;} public void print() { System.out.println("Name: " + name); System.out.println("Pay Rate: "+payRate);

Review of inheritance (contd.) Now, suppose we wish to define another class to represent a part-time employee whose salary is paid per hour. We inherit from the Employee class as follows: class HourlyEmployee extends Employee { private int hours; public HourlyEmployee(String hName, double hRate) { super(hName, hRate); hours = 0; } public void addHours(int moreHours) {hours += moreHours;} public double pay() {return payRate * hours;} public void print() { super.print(); System.out.println("Current hours: " + hours);

Notes about Inheritance We observe the following from the examples on inheritance: Methods and instance variables of the super class are inherited by subclasses, thus allowing for code reuse. A subclass can define additional instance variables (e.g. hours) and additional methods (e.g. addHours). A subclass can override some of the methods of the super class to make them behave differently (e.g. the pay & print) Constructors are not inherited, but can be called using the super keyword. such a call must be the first statement. If the constructor of the super class is not called, then the complier inserts a call to the default constructor -watch out! super may also be used to call a method of the super class.

Review of Abstract Classes Inheritance enforces hierarchical organization, the benefit of which are: reusability, type sharing and polymorphism. Java uses Abstract classes & Interfaces to further strengthen the idea of inheritance. To see the role of abstract of classes, suppose that the pay method is not implemented in the HourlyEmployee subclass. Obviously, the pay method in the Employee class will be assumed, which will lead to wrong result. One solution is to remove the pay method out and put it in another extension of the Employee class, MonthlyEmployee. The problem with this solution is that it does not force subclasses of Employee class to implement the pay method.

Review of Abstract Classes (Cont'd) The solution is to declare the pay method of the Employee class as abstract, thus, making the class abstract. abstract class Employee { protected String name; protected double payRate; public Employee(String empName, double empRate) { name = empName; payRate = empRate; } public String getName() {return name;} public void setPayRate(double newRate) {payRate = newRate;} abstract public double pay(); public void print() { System.out.println("Name: " + name); System.out.println("Pay Rate: "+payRate);

Review of Abstract Classes (Cont'd) The following extends the Employee abstract class to get MonthlyEmployee class. The next example extends the MonthlyEmployee class to get the Executive class. class MonthlyEmployee extends Employee { public MonthlyEmployee(String empName, double empRate) { super(empName, empRate); } public double pay() { return payRate;

Review of Abstract Classes (Cont'd) class Executive extends MonthlyEmployee { private double bonus; public Executive(String exName, double exRate) { super(exName, exRate); bonus = 0; } public void awardBonus(double amount) { bonus = amount; public double pay() { double paycheck = super.pay() + bonus; return paycheck; public void print() { super.print(); System.out.println("Current bonus: " + bonus); HourlyEmployee Employee MonthlyEmployee Executive

Review of Abstract Classes (Cont'd) The following further illustrates the advantages of organizing classes using inheritance - same type, polymorphism, etc. public class TestAbstractClass { public static void main(String[] args) { Employee[] list = new Employee[3]; list[0] = new Executive("Jarallah Al-Ghamdi", 50000); list[1] = new HourlyEmployee("Azmat Ansari", 120); list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000); ((Executive)list[0]).awardBonus(11000); for(int i = 0; i < list.length; i++) if(list[i] instanceof HourlyEmployee) ((HourlyEmployee)list[i]).addHours(60); for(int i = 0; i < list.length; i++) { list[i].print(); System.out.println("Paid: " + list[i].pay()); System.out.println("*************************"); } The Program Output

Review of Interfaces Interfaces are not classes, they are entirely a separate entity. They provide a list of abstract methods which MUST be implemented by a class that implements the interface. Unlike abstract classes which may contain implementation of some of the methods, interfaces provide NO implementation. Like abstract classes, the purpose of interfaces is to provide organizational structure. More importantly, interfaces are here to provide a kind of "multiple inheritance" which is not supported in Java. If both parents of a child implement a method, which one does the child inherits? - Multiple inheritance confusion. Interfaces allow a child to be both of type A and B.

Review of Interfaces (contd.) Recall that Java has the Comparable interface defined as: Recall also that java has the java.util.Arrays class, which has a sort method that can sort any array whose contents are either primitive values or Comparable objects. Thus, to sort our list of Employee objects, all we need is to modify the Employee class to implement the Comparable interface. Notice that this will work even if the Employee class is extending another class or implementing another interface. This modification is shown in the next page. interface Comparable { int compareTo(Object o); }

Review of Interfaces (contd.) abstract class Employee implements Comparable { protected String name; protected double payRate; public Employee(String empName, double empRate) { name = empName; payRate = empRate; } public String getName() {return name;} public void setPayRate(double newRate) { payRate = newRate; abstract public double pay(); public int compareTo(Object o) { Employee e = (Employee) o; return name.compareTo( e.getName()); HourlyEmployee Comparable Employee Executive MonthlyEmployee

Review of Interfaces (contd.) Since Employee class implements the Comparable interface, the array of employees can now be sorted as shown below: import java.util.Arrays; public class TestInterface { public static void main(String[] args) { Employee[] list = new Employee[3]; list[0] = new Executive("Jarallah Al-Ghamdi", 50000); list[1] = new HourlyEmployee("Azmat Ansari", 120); list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000); ((Executive)list[0]).awardBonus(11000); for(int i = 0; i < list.length; i++) if(list[i] instanceof HourlyEmployee) ((HourlyEmployee)list[i]).addHours(60); Arrays.sort(list); for(int i = 0; i < list.length; i++) { list[i].print(); System.out.println("Paid: " + list[i].pay()); System.out.println("**********************"); } The program output

Review Questions How does an interface differ from an abstract class? Why does Java not support multiple inheritance? What feature of Java helps realize the benefits of multiple inheritance? An Abstract class must contain at least one abstract method, (true or false)? A subclass typically represents a larger number of objects than its super class, (true or false)? A subclass typically encapsulates less functionality than its super class does, (true or false)? An instance of a class can be assigned to a variable of type any of the interfaces the class implements, (true or false)?