Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
CHAPTER 1 Object-Oriented Programming and Class Hierarchies.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance and Class Hierarchies Chapter 3 Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Inheritance using Java
CHAPTER 3 Inheritance and Class Hierarchies. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how C++
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives  Learn what is an ADT, why is it useful  Understand algorithm efficiency  Use Eclipse for coding and testing  Understand and use.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSC 142 Computer Science II Zhen Jiang West Chester University
Lists and the Collection Interface Review inheritance & collections.
Programming in Java CSCI-2220 Object Oriented Programming.
CIS 2168 Data Structures and Algorithms in JAVA Object-Oriented Programming and Class Hierarchies.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
CIS 2168 Data Structures and Algorithms
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
Friday, January 26, 2018 Announcements… For Today… For Next Time…
Inheritance and Class Hierarchies
One class is an extension of another.
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Interfaces.
Java Inheritance.
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Data Structures and Algorithms Prof. Nadeem Abdul Hamid CSC 220 - Fall 2005 Lecture Unit 3 - Inheritance

Inheritance Besides reusing existing classes for new applications, OOP allows definition of new classes that extend existing ones to provide additional functionality Subclass inherits data fields and methods of the superclass In Java, all classes part of inheritance hierarchy Class Object is the superclass of all Java classes

Class Hierarchy

Is-A vs. Has-A Relationships “Jet plane is an airplane” JetPlane class will extend Airplane “Jet plane has a jet engine” JetPlane class will have a JetEngine field public class JetPlane extends Airplane { private JetEngine[] jets; ... }

Superclass/Subclass Example /** Class that represents a computer. * @author Koffman & Wolfgang * */ public class Computer { // Data Fields private String manufacturer; private String processor; private int ramSize; private int diskSize; // Methods /** Initializes a Computer object with all properties specified. @param man The computer manufacturer @param processor The processor type @param ram The RAM size @param disk The disk size */ public Computer(String man, String processor, int ram, int disk) { manufacturer = man; this.processor = processor; ramSize = ram; diskSize = disk; } // Insert other accessor and modifier methods here. public String toString() { String result = "Manufacturer: " + manufacturer + "\nCPU: " + processor + "\nRAM: " + ramSize + " megabytes" + "\nDisk: " + diskSize + " gigabytes"; return result;

Subclass - Laptop /** Class that represents a lap top computer. * @author Koffman & Wolfgang * */ public class LapTop extends Computer { // Data Fields private static final String DEFAULT_LT_MAN = "MyBrand"; private double screenSize; private double weight; /** Initializes a LapTop object with all properties specified. @param man The computer manufacturer @param proc The processor type @param ram The RAM size @param disk The disk size @param screen The screen size @param wei The weight */ public LapTop(String man, String proc, int ram, int disk, double screen, double wei) { super(man, proc, ram, disk); screenSize = screen; weight = wei; }

Inheritance Issues Use of this. Initializing data fields in subclass Use of super() No-parameter constructor protected visibility

Method Overriding public class TestComputerAndLaptop { /** Tests classes Computer and LapTop. Creates an object of each and displays them. @param args[] No control parameters @author Koffman & Wolfgang */ public static void main(String[] args) { Computer myComputer = new Computer("Acme", "Intel P4 2.4", 512, 60); LapTop yourComputer = new LapTop("DellGate", "AMD Athlon 2000", 256, 40, 15.0, 7.5); System.out.println("My computer is:\n" + myComputer.toString()); System.out.println("\nYour computer is:\n" + yourComputer.toString()); } My computer is: Manufacturer: Acme CPU: Intel P4 2.4 RAM: 512 megabytes Disk: 60 gigabytes Your computer is: Manufacturer: DellGate CPU: AMD Athlon 2000 RAM: 256 megabytes Disk: 40 gigabytes

Method Overriding In the Laptop class: public String toString() { String result = super.toString() + "\nScreen size: " + screenSize + " inches" + "\nWeight: " + weight + " pounds"; return result; } My computer is: Manufacturer: Acme CPU: Intel P4 2.4 RAM: 512 megabytes Disk: 60 gigabytes Your computer is: Manufacturer: DellGate CPU: AMD Athlon 2000 RAM: 256 megabytes Disk: 40 gigabytes Screen size: 15.0 inches Weight: 7.5 pounds

Method Overloading /** Initializes a LapTop object with all properties specified. @param man The computer manufacturer @param proc The processor type @param ram The RAM size @param disk The disk size @param screen The screen size @param wei The weight */ public LapTop(String man, String proc, int ram, int disk, double screen, double wei) { super(man, proc, ram, disk); screenSize = screen; weight = wei; } /** Initializes a LapTop object with 5 properties specified. */ public LapTop(String proc, int ram, int disk, this(DEFAULT_LT_MAN, proc, ram, disk, screen, wei);

Polymorphism Important feature of OOP languages Object variable may contain reference to the specified class, or any subclass thereof Enables JVM to determine which (overridden) method to invoke at runtime based on type of the object reference Computer comp[] = new Computer[3]; comp[0] = new Computer("Acme", "Intel P4 2.4", 512, 60); comp[1] = new LapTop("DellGate", "AMD Athlon 2000", 256, 40, 15.0, 7.5); comp[2] = comp[0]; ... for ( int i = 0; i < comp.length; i++ ) System.out.println( "Computer " + (i+1) + ": \n" + comp[i] );

Abstract Classes Can declare abstract methods like an interface, which must be implemented by subclasses Cannot be instantiated Can have field definitions and method bodies

Summary Table

Multiple Inheritance Multiple inheritance: the ability to extend more than one class Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity Therefore, Java does not allow a class to extend more than one class

Using Multiple Interfaces If we define two interfaces, a class can implement both Multiple interfaces emulate multiple inheritance

Implementing Reuse Through Delegation You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation In delegation, a method of one class accomplishes an operation by delegating it to a method of another class

Packages The Java API is organized into packages The package to which a class belongs is declared by the first statement in the file in which the class is defined using the keyword package followed by the package name All classes in the same package are stored in the same directory or folder All the classes in one folder must declare themselves to be in the same package Classes that are not part of a package may access only public members of classes in the package

The No-Package-Declared Environment and Package Visibility There exists a default package Files that do specify a package are considered part of the default package If you don’t declare packages, all of your packages belong to the same, default package Package visibility sits between private and protected Classes, data fields, and methods with package visibility are accessible to all other methods of the same package but are not accessible to methods outside of the package Classes, data fields, and methods that are declared protected are visible to all members of the package

Visibility (Access Controls)