SlideShare a Scribd company logo
1 of 14
CSE 143
Lecture 3
Inheritance
slides created by Marty Stepp
http://www.cs.washington.edu/143/
2
More ArrayIntList
• Let's add some new features to our ArrayIntList class:
1. A method that allows client programs to print a list's elements
2. A constructor that accepts an initial capacity
(By writing these we will recall some features of objects in Java.)
• Printing lists: You may be tempted to write a print method:
// client code
ArrayIntList list = new ArrayIntList();
...
list.print();
– Why is this a bad idea? What would be better?
3
The toString method
• Tells Java how to convert an object into a String
ArrayIntList list = new ArrayIntList();
System.out.println("list is " + list);
// ("list is " + list.toString());
• Syntax:
public String toString() {
code that returns a suitable String;
}
• Every class has a toString, even if it isn't in your code.
– The default is the class's name and a hex (base-16) number:
ArrayIntList@9e8c34
4
toString solution
// Returns a String representation of the list.
public String toString() {
if (size == 0) {
return "[]";
} else {
String result = "[" + elementData[0];
for (int i = 1; i < size; i++) {
result += ", " + elementData[i];
}
result += "]";
return result;
}
}
5
Multiple constructors
• existing constructor:
public ArrayIntList() {
elementData = new int[1000];
size = 0;
}
• Add a new constructor that accepts a capacity parameter:
public ArrayIntList(int capacity) {
elementData = new int[capacity];
size = 0;
}
– The constructors are very similar. Can we avoid redundancy?
6
this keyword
• this : A reference to the implicit parameter
(the object on which a method/constructor is called)
• Syntax:
– To refer to a field: this.field
– To call a method: this.method(parameters);
– To call a constructor this(parameters);
from another constructor:
7
Revised constructors
public ArrayIntList(int capacity) {
elementData = new int[capacity];
size = 0;
}
public ArrayIntList() {
this(1000); // calls other constructor
}
8
Exercise
• Write a class called StutterIntList.
– Its constructor accepts an integer stretch parameter.
– Every time an integer is added, the list will actually add stretch
number of copies of that integer.
• Example usage:
StutterIntList list = new StutterIntList(3);
list.add(7); // [7, 7, 7]
list.add(-1); // [7, 7, 7, -1, -1, -1]
list.add(2, 5); // [7, 7, 5, 5, 5, 7, -1, -1, -1]
list.remove(4); // [7, 7, 5, 5, 7, -1, -1, -1]
System.out.println(list.getStretch()); // 3
9
Inheritance
• inheritance: Forming new classes based on existing ones.
– a way to share/reuse code between two or more classes
– superclass: Parent class being extended.
– subclass: Child class that inherits behavior from superclass.
• gets a copy of every field and method from superclass
– is-a relationship: Each object of the subclass also "is a(n)"
object of the superclass and can be treated as one.
10
Inheritance syntax
public class name extends superclass {
– Example:
public class Lawyer extends Employee {
...
}
• By extending Employee, each Lawyer object now:
– receives a copy of each method from Employee automatically
– can be treated as an Employee by client code
11
Overriding methods
• override: To replace a superclass's method by writing a new
version of that method in a subclass.
– No special syntax is required to override a method.
Just write a new version of it in the subclass.
public class Lawyer extends Employee {
// overrides getSalary method in Employee class;
// give Lawyers a $5K raise
public double getSalary() {
return 55000.00;
}
}
12
super keyword
• Subclasses can call overridden methods with super
super.method(parameters)
– Example:
public class Lawyer extends Employee {
// give Lawyers a $5K raise (better)
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + 5000.00;
}
}
– This version makes sure that Lawyers always make $5K more
than Employees, even if the Employee's salary changes.
13
Calling super constructor
super(parameters);
– Example:
public class Lawyer extends Employee {
public Lawyer(int years) {
super(years); // calls Employee constructor
}
...
}
– The super call must be the first statement in the constructor.
– Constructors are not inherited; If you extend a class, you must
write all the constructors you want your subclass to have.
14
Exercise solution
public class StutterIntList extends ArrayIntList {
private int stretch;
public StutterIntList(int stretchFactor) {
super();
stretch = stretchFactor;
}
public StutterIntList(int stretchFactor, int capacity) {
super(capacity);
stretch = stretchFactor;
}
public void add(int value) {
for (int i = 1; i <= stretch; i++) {
super.add(value);
}
}
public void add(int index, int value) {
for (int i = 1; i <= stretch; i++) {
super.add(index, value);
}
}
public int getStretch() {
return stretch;
}
}

More Related Content

Similar to 03-inheritance.ppt

Similar to 03-inheritance.ppt (20)

Dependency Injection in Spring
Dependency Injection in SpringDependency Injection in Spring
Dependency Injection in Spring
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
 
C#2
C#2C#2
C#2
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Synapse india complain sharing info on chapter 8 operator overloading
Synapse india complain sharing info on chapter 8   operator overloadingSynapse india complain sharing info on chapter 8   operator overloading
Synapse india complain sharing info on chapter 8 operator overloading
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 

More from SaiM947604

Software development lifestyle cycle. .
Software development lifestyle cycle.  .Software development lifestyle cycle.  .
Software development lifestyle cycle. .SaiM947604
 
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...SaiM947604
 
22R01A66C6 DSP.pptx
22R01A66C6 DSP.pptx22R01A66C6 DSP.pptx
22R01A66C6 DSP.pptxSaiM947604
 
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptxSaiM947604
 
presentationrenewableenergyresources-190331151749 (1).pdf
presentationrenewableenergyresources-190331151749 (1).pdfpresentationrenewableenergyresources-190331151749 (1).pdf
presentationrenewableenergyresources-190331151749 (1).pdfSaiM947604
 
Non_and_renwable_resources_ppt.ppt
Non_and_renwable_resources_ppt.pptNon_and_renwable_resources_ppt.ppt
Non_and_renwable_resources_ppt.pptSaiM947604
 
IT Project1.pptx
IT Project1.pptxIT Project1.pptx
IT Project1.pptxSaiM947604
 

More from SaiM947604 (7)

Software development lifestyle cycle. .
Software development lifestyle cycle.  .Software development lifestyle cycle.  .
Software development lifestyle cycle. .
 
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...
Apply Raw Data Set And Implement The Different Data Warngliing Functionalitie...
 
22R01A66C6 DSP.pptx
22R01A66C6 DSP.pptx22R01A66C6 DSP.pptx
22R01A66C6 DSP.pptx
 
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx
𝓒𝓱𝓮𝓶𝓲𝓼𝓽𝓻𝔂 𝓹𝓹𝓽..pptx
 
presentationrenewableenergyresources-190331151749 (1).pdf
presentationrenewableenergyresources-190331151749 (1).pdfpresentationrenewableenergyresources-190331151749 (1).pdf
presentationrenewableenergyresources-190331151749 (1).pdf
 
Non_and_renwable_resources_ppt.ppt
Non_and_renwable_resources_ppt.pptNon_and_renwable_resources_ppt.ppt
Non_and_renwable_resources_ppt.ppt
 
IT Project1.pptx
IT Project1.pptxIT Project1.pptx
IT Project1.pptx
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

03-inheritance.ppt

  • 1. CSE 143 Lecture 3 Inheritance slides created by Marty Stepp http://www.cs.washington.edu/143/
  • 2. 2 More ArrayIntList • Let's add some new features to our ArrayIntList class: 1. A method that allows client programs to print a list's elements 2. A constructor that accepts an initial capacity (By writing these we will recall some features of objects in Java.) • Printing lists: You may be tempted to write a print method: // client code ArrayIntList list = new ArrayIntList(); ... list.print(); – Why is this a bad idea? What would be better?
  • 3. 3 The toString method • Tells Java how to convert an object into a String ArrayIntList list = new ArrayIntList(); System.out.println("list is " + list); // ("list is " + list.toString()); • Syntax: public String toString() { code that returns a suitable String; } • Every class has a toString, even if it isn't in your code. – The default is the class's name and a hex (base-16) number: ArrayIntList@9e8c34
  • 4. 4 toString solution // Returns a String representation of the list. public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } }
  • 5. 5 Multiple constructors • existing constructor: public ArrayIntList() { elementData = new int[1000]; size = 0; } • Add a new constructor that accepts a capacity parameter: public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } – The constructors are very similar. Can we avoid redundancy?
  • 6. 6 this keyword • this : A reference to the implicit parameter (the object on which a method/constructor is called) • Syntax: – To refer to a field: this.field – To call a method: this.method(parameters); – To call a constructor this(parameters); from another constructor:
  • 7. 7 Revised constructors public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } public ArrayIntList() { this(1000); // calls other constructor }
  • 8. 8 Exercise • Write a class called StutterIntList. – Its constructor accepts an integer stretch parameter. – Every time an integer is added, the list will actually add stretch number of copies of that integer. • Example usage: StutterIntList list = new StutterIntList(3); list.add(7); // [7, 7, 7] list.add(-1); // [7, 7, 7, -1, -1, -1] list.add(2, 5); // [7, 7, 5, 5, 5, 7, -1, -1, -1] list.remove(4); // [7, 7, 5, 5, 7, -1, -1, -1] System.out.println(list.getStretch()); // 3
  • 9. 9 Inheritance • inheritance: Forming new classes based on existing ones. – a way to share/reuse code between two or more classes – superclass: Parent class being extended. – subclass: Child class that inherits behavior from superclass. • gets a copy of every field and method from superclass – is-a relationship: Each object of the subclass also "is a(n)" object of the superclass and can be treated as one.
  • 10. 10 Inheritance syntax public class name extends superclass { – Example: public class Lawyer extends Employee { ... } • By extending Employee, each Lawyer object now: – receives a copy of each method from Employee automatically – can be treated as an Employee by client code
  • 11. 11 Overriding methods • override: To replace a superclass's method by writing a new version of that method in a subclass. – No special syntax is required to override a method. Just write a new version of it in the subclass. public class Lawyer extends Employee { // overrides getSalary method in Employee class; // give Lawyers a $5K raise public double getSalary() { return 55000.00; } }
  • 12. 12 super keyword • Subclasses can call overridden methods with super super.method(parameters) – Example: public class Lawyer extends Employee { // give Lawyers a $5K raise (better) public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + 5000.00; } } – This version makes sure that Lawyers always make $5K more than Employees, even if the Employee's salary changes.
  • 13. 13 Calling super constructor super(parameters); – Example: public class Lawyer extends Employee { public Lawyer(int years) { super(years); // calls Employee constructor } ... } – The super call must be the first statement in the constructor. – Constructors are not inherited; If you extend a class, you must write all the constructors you want your subclass to have.
  • 14. 14 Exercise solution public class StutterIntList extends ArrayIntList { private int stretch; public StutterIntList(int stretchFactor) { super(); stretch = stretchFactor; } public StutterIntList(int stretchFactor, int capacity) { super(capacity); stretch = stretchFactor; } public void add(int value) { for (int i = 1; i <= stretch; i++) { super.add(value); } } public void add(int index, int value) { for (int i = 1; i <= stretch; i++) { super.add(index, value); } } public int getStretch() { return stretch; } }