COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)
|
|
- Mervyn Webster
- 5 years ago
- Views:
Transcription
1 COE318 Lecture Notes: Week 9 1 of 14 COE318 Lecture Notes Week 9 (Week of Oct 29, 2012) Topics The final keyword Inheritance and Polymorphism The final keyword Zoo: Version 1 This simple version models a Zoo that contains an arbitrary number of Lions. There is nothing new here. It's just a starting point for exploring, using and understanding some important features of object-oriented programming. But, some notes... The instance variables of Lion are private and final. Consequently, Lion objects are (for the moment) immutable. public class Lion { private final String name; private final int birthyear; public Lion(String name, int year) { this.name = name; birthyear = year;
2 COE318 Lecture Notes: Week 9 2 of 14 public int age() { return birthyear; public String getname() { return name; public String noise() { return "GGGrowl"; public boolean eatspeople() { return true; import java.util.arraylist; public class Zoo { private final ArrayList<Lion> lions = new ArrayList<Lion>(); public void add(lion lion) { lions.add(lion); public String getnames() { String s = ""; for(lion leo : lions) { s += leo.getname() + "\n"; return s; public static void main(string[] args) { Zoo zoo = new Zoo(); Lion a = new Lion("Alex", 2010);
3 COE318 Lecture Notes: Week 9 3 of 14 zoo.add(a); zoo.add(new Lion("Betty", 2009)); System.out.println(zoo.getNames()); Zoo Version 2: A First Look at Inheritance The zoo wants to have more animals than just lions. But proceeding as before would be very tedious if, say, there were 100 different kinds of animals: We'd need to create Pig, Cat, Crocodile, Elephant, Hummingbird, etc. classes and they would all have just about the same code; We'd need separate ArrayLists for each type of animal in the Zoo class. It would be difficult to treat all the animals as a group: to sort them alphabetically by name or by age, for example. Inheritance (subclassing) to the rescue! We can put practically all the code for Lion into another class we call AbstractAnimal. (Why not call it Animal instead of AbstractAnimal? Good question! To be explained in the next version.) We can then rewrite the Lion class to extend the AbstractAnimal class; all of its methods are inherited by Lion and do not have to be copied and pasted. The Lion class now looks like: public class Lion extends AbstractAnimal { public Lion(String name, int year) { super(name, year); But what's this super(name, year); stuff?
4 COE318 Lecture Notes: Week 9 4 of 14 It invokes the constructor (with the same signature) of its superclass. It is not always necessary to invoke the superclass constructor as is done here; however, If your do, it must be the first statement in the subclass's constructor. The superclass is AbstractAnimal and here's what it looks like: public class AbstractAnimal { private final String name; private final int birthyear; public AbstractAnimal(String name, int year) { this.name = name; birthyear = year; public int age() { return birthyear; public String getname() { return name; public String noise() { return "GGGrowl"; public boolean eatspeople() { return true; We can now write the Hedgehog class. However, the inherited methods eatspeople() and noise() give the wrong answer for a hedgehog. An inherited method can be overridden by simply re-writing it. (A subclass almost always overrides at least one method from its superclass.) Note: The line is not required. It can be useful for the compiler and some tools. Netbeans recommends you put it in. It also reminds programmers that they are modifying an inherited method.) In general, lines beginning are called annotations. Programmers can define their own annotations but that is beyond the scope of this course.)
5 COE318 Lecture Notes: Week 9 5 of 14 public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year); public boolean eatspeople() { return false; How do we declare the data type of a Lion or Hedgehog? In the past (before inheritance), this question would appear foolish. For example, we would write things like: Resistor r; r = new Resistor(10); r.setvoltage(10); There would be no other choice. But with inheritance we can declare a Lion as either of type Lion or type AbstractAnimal: Lion p = new Lion( Leo ); AbstractAnimal q = new Lion( Simba ); There is absolutely no difference between how p and q behave. They are both Lions and behave as such no matter how their data types were declared. And there is no difference in runtime efficiency. But declaring Lions or Hedgehogs or Pigs to be AbstractAnimals has big advantages. For example, the Zoo class can keep all of the animals in a single ArrayList<AbstractAnimal>. Below is the new, improved Zoo class: import java.util.arraylist; public class Zoo { private final ArrayList<AbstractAnimal> animals = new ArrayList<AbstractAnimal>();
6 COE318 Lecture Notes: Week 9 6 of 14 public void add(abstractanimal animal) { animals.add(animal); public String getnames() { String s = ""; for (AbstractAnimal a : animals) { s += a.getname() + "\n"; return s; public AbstractAnimal[] getanimals() { AbstractAnimal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) { Zoo zoo = new Zoo(); Lion a = new Lion("Leo", 2010); zoo.add(a); zoo.add(new Lion("Simba", 2009)); zoo.add(new Hedgehog("Sonic", 2010)); System.out.println(zoo.getNames()); AbstractAnimal[] animals = zoo.getanimals(); for (AbstractAnimal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat") + " people"); Zoo Version 3: Abstract classes and methods AbstractAnimal is just a normal class and it is legal to write: AbstractAnimal a = new AbstractAnimal( Sue );
7 COE318 Lecture Notes: Week 9 7 of 14 This should not be allowed! It makes no sense to create an animal when we don't know what kind of animal it is. AbstractAnimal exists only for the purpose of subclassing. We can make this clear by declaring it to be abstract. The compiler will now disallow: AbstractAnimal a = new AbstractAnimal( Sue ); We now refer to AbstractAnimal as an abstract class that cannot be instantiated. But Lion and Hedgehog are concrete classes that can be instantiated. We can also declare methods to be abstract. If we do so, we must declare the class to be abstract. Furthermore, when we extend an abstract class as a concrete class, the compiler will insist that we write implementation code for any abstract methods. The code for our classes is given below. Note: the Zoo class needs no changes. The AbstractAnimal class is now declared abstract as is its noise() method. Both Lion and Hedgehog now have to implement (and not just inherit) the noise() method. public abstract class AbstractAnimal { private final String name; private final int birthyear; public AbstractAnimal(String name, int year) { this.name = name; birthyear = year; public int age() { return birthyear; public String getname() { return name; public abstract String noise(); public boolean eatspeople() { return true;
8 COE318 Lecture Notes: Week 9 8 of 14 public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year); public boolean eatspeople() { return false; public String noise() { return "Chirp...chirp"; public class Lion extends AbstractAnimal { public Lion(String name, int year) { super(name, year); public String noise() { return "Rrroar (yum yum)"; Zoo Version 4: Interfaces The zoo owner is happy with version 3; lots and lots of concrete animal classes can be easily added; no changes needed to the Zoo or AbstractAnimal classes. But then one day he says, I love my fridge. I want to have it as one of the amimals in my zoo.
9 COE318 Lecture Notes: Week 9 9 of 14 The guy may be a lunatic, but we have to respect his wishes. Tell him just to subclass Refrigerator from AbstractAnimal. But he can't do that; Refriger ator is already subclassed from ElectricalAppliance. OK, we'll make an interface called Animal. He just has to implement the interface. What is an interface? An interface is simply a collection of zero or more public abstract methods. A class can implement as many interfaces as it wants. It just has to ensure that all the abstract methods are actually implemented. (And the compiler will ensure that this is done.) We also take advantage of the Animal interface and have AbstractAnimal implement it. Objects can be declared as their own type or as any of their superclass's type. In addition, an object can be declared as any interface type implemented by the actual class. Consequently, we can now declare all the animals (and the fridge) as type Animal. We also have AbstractAnimal implement a standard imterface, Comparable<Animal>. Note (lab 5): You are provided with an interface in lab 5: UserInterface which describes what the methods should do. The reason for this so that any user interface has the same mthods and can be easily plugged into the existing code. (A graphical user interface would have buttons and show the cards in a graphical way. A networked interface coudl also be written.) interface Animal { String noise(); boolean eatspeople(); int age(); String getname(); public abstract class AbstractAnimal implements Animal, Comparable<Animal> { private final String name; private final int birthyear; public AbstractAnimal(String name, int year) { this.name = name; birthyear = year;
10 COE318 Lecture Notes: Week 9 10 of 14 public int age() { return birthyear; public String getname() { return name; public abstract String noise(); public boolean eatspeople() { return this instanceof Dangerous; public int compareto(animal other) { return getname().compareto(other.getname()); public boolean equals(object obj) { if (obj == null) { return false; if (getclass()!= obj.getclass()) { return false; final AbstractAnimal other = (AbstractAnimal) obj; if ((this.name == null)? (other.name!= null) :!this.name.equals(other.name)) { return false; return true; public int hashcode() { int hash = 7; hash = 83 * hash
11 COE318 Lecture Notes: Week 9 11 of 14 + (this.name!= null? this.name.hashcode() : 0); return hash; public interface Dangerous { public class Lion extends AbstractAnimal implements Dangerous{ public Lion(String name, int year) { super(name, year); public String noise() { return "GGGrowl"; public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year); public String noise() { return "Chirp chirp";
12 COE318 Lecture Notes: Week 9 12 of 14 import java.util.arraylist; public class Zoo { private final ArrayList<Animal> animals = new ArrayList<Animal>(); public void add(animal animal) { animals.add(animal); public String getnames() { String s = ""; for (Animal a : animals) { s += a.getname() + "\n"; return s; public Animal[] getanimals() { Animal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) { Zoo zoo = new Zoo(); Animal a = new Lion("Alex", 2010); zoo.add(a); zoo.add(new Lion("Betty", 2009)); zoo.add(new Hedgehog("Charlie", 2010)); System.out.println(zoo.getNames()); Animal[] animals = zoo.getanimals(); for (Animal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat") + " people");
13 COE318 Lecture Notes: Week 9 13 of 14 Zoo Version 5: Exceptions import java.util.arraylist; public class Zoo { private final ArrayList<Animal> animals = new ArrayList<Animal>(); public void add(animal animal) { if (animals.contains(animal)) { throw new IllegalArgumentException("Duplicate name not allowed"); animals.add(animal); public String getnames() { String s = ""; for (Animal a : animals) { s += a.getname() + "\n"; return s; public Animal[] getanimals() { Animal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) { Zoo zoo = new Zoo(); Animal a = new Lion("Alex", 2010); zoo.add(a); zoo.add(new Lion("Betty", 2009)); Animal charlie1 = new Hedgehog("Charlie", 2010); zoo.add(charlie1);
14 COE318 Lecture Notes: Week 9 14 of 14 System.out.println(zoo.getNames()); Animal charlie2 = new Lion("Charlie", 2007); added"); try { zoo.add(charlie2); catch (IllegalArgumentException ex) { System.err.println("Duplicate name not Animal[] animals = zoo.getanimals(); for (Animal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat") + " people");
COE318 Lecture Notes Week 8 (Oct 24, 2011)
COE318 Software Systems Lecture Notes: Week 8 1 of 17 COE318 Lecture Notes Week 8 (Oct 24, 2011) Topics == vs..equals(...): A first look Casting Inheritance, interfaces, etc Introduction to Juni (unit
More informationCOE318 Lecture Notes Week 9 (Oct 31, 2011)
COE318 Software Systems Lecture Notes: Week 9 1 of 12 COE318 Lecture Notes Week 9 (Oct 31, 2011) Topics Casting reference variables equals() and hashcode() overloading Collections and ArrayList utilities
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationAbstract Classes and Interfaces
Abstract Classes and Interfaces Reading: Reges and Stepp: 9.5 9.6 CSC216: Programming Concepts Sarah Heckman 1 Abstract Classes A Java class that cannot be instantiated, but instead serves as a superclass
More informationAnnouncement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August
Polymorphism, Dynamic Binding and Interface 2 4 pm Thursday 7/31/2008 @JD2211 1 Announcement Next week is off The class will continue on Tuesday, 12 th August 2 Agenda Review Inheritance Abstract Array
More informationJava Magistère BFA
Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt
More informationPolymorphism. return a.doublevalue() + b.doublevalue();
Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces
More informationInheritance (Outsource: )
(Outsource: 9-12 9-14) is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, inherit attributes and behavior of the pre-existing classes,
More informationDistributed Systems Recitation 1. Tamim Jabban
15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming
More informationInheritance (Part 5) Odds and ends
Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there
More informationCMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.
CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object
More informationObject-Oriented Programming More Inheritance
Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 1 / 45 1 Inheritance Flat Hierarchy
More informationArgument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar
More informationCOE318 Lecture Notes Week 6 (Oct 10, 2011)
COE318 Software Systems Lecture Notes: Week 6 1 of 8 COE318 Lecture Notes Week 6 (Oct 10, 2011) Topics Announcements final qualifiers Example: An alternative to arrays == vs..equals(...): A first look
More informationCOE318 Lecture Notes Week 10 (Nov 7, 2011)
COE318 Software Systems Lecture Notes: Week 10 1 of 5 COE318 Lecture Notes Week 10 (Nov 7, 2011) Topics More about exceptions References Head First Java: Chapter 11 (Risky Behavior) The Java Tutorial:
More informationINHERITANCE. Spring 2019
INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose
More informationAdvanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II
Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic
More informationDay 4. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information
More informationLogistics. Final Exam on Friday at 3pm in CHEM 102
Java Review Logistics Final Exam on Friday at 3pm in CHEM 102 What is a class? A class is primarily a description of objects, or instances, of that class A class contains one or more constructors to create
More informationThe Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000
The Object Class Mark Allen Weiss Copyright 2000 1/4/02 1 java.lang.object All classes either extend Object directly or indirectly. Makes it easier to write generic algorithms and data structures Makes
More informationJava Fundamentals (II)
Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class
More informationWhat is Inheritance?
Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting
More informationBuilding Java Programs. Inheritance and Polymorphism
Building Java Programs Inheritance and Polymorphism Input and output streams stream: an abstraction of a source or target of data 8-bit bytes flow to (output) and from (input) streams can represent many
More informationCOMP200 INTERFACES. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE
More informationObject-Oriented ProgrammingInheritance & Polymorphism
Inheritance Polymorphism Overriding and Overloading Object-Oriented Programming Inheritance & Polymorphism Inf1 :: 2008/09 Inheritance Polymorphism Overriding and Overloading 1 Inheritance Flat Hierarchy
More informationRules and syntax for inheritance. The boring stuff
Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for
More informationAP CS Unit 6: Inheritance Notes
AP CS Unit 6: Inheritance Notes Inheritance is an important feature of object-oriented languages. It allows the designer to create a new class based on another class. The new class inherits everything
More informationObject-Oriented Concepts
JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing
More informationFirst IS-A Relationship: Inheritance
First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class
More informationInheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.
Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods
More informationMore About Objects. Zheng-Liang Lu Java Programming 255 / 282
More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism
More informationInf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.
Inf1-OP Inheritance UML Class Diagrams UML: language for specifying and visualizing OOP software systems UML class diagram: specifies class name, instance variables, methods,... Volker Seeker, adapting
More informationInheritance. Lecture 11 COP 3252 Summer May 25, 2017
Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data
More informationSoftware Development (cs2500)
Software Development (cs2500) Lecture 31: Abstract Classes and Methods M.R.C. van Dongen January 12, 2011 Contents 1 Outline 1 2 Abstract Classes 1 3 Abstract Methods 3 4 The Object Class 4 4.1 Overriding
More informationInf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics
Inf1-OP Inheritance Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 12, 2018 UML Class Diagrams UML: language for specifying and visualizing OOP software
More informationObject Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)
Object Oriented Programming Part II of II Steve Ryder Session 8352 JSR Systems (JSR) sryder@jsrsys.com New Terms in this Section API Access Modifier Package Constructor 2 Polymorphism Three steps of object
More informationChapter 14 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden
More informationBasic Object-Oriented Concepts. 5-Oct-17
Basic Object-Oriented Concepts 5-Oct-17 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains
More informationBig software. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting
More informationOVERRIDING. 7/11/2015 Budditha Hettige 82
OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of
More informationCLASS DESIGN. Objectives MODULE 4
MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors
More informationDistributed Systems Recitation 1. Tamim Jabban
15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming
More informationJAVA MOCK TEST JAVA MOCK TEST II
http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your
More informationJava Classes, Inheritance, and Interfaces
Java Classes, Inheritance, and Interfaces Introduction Classes are a foundational element in Java. Everything in Java is contained in a class. Classes are used to create Objects which contain the functionality
More informationSuper-Classes and sub-classes
Super-Classes and sub-classes Subclasses. Overriding Methods Subclass Constructors Inheritance Hierarchies Polymorphism Casting 1 Subclasses: Often you want to write a class that is a special case of an
More informationIntroduction to Inheritance
Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits
More informationEncapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass
Encapsulation Again Inheritance Encapsulation and Inheritance Inf1-OOP Inheritance and Interfaces Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 9, 2015 The Object
More informationInf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics
Inf1-OOP Inheritance and Interfaces Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Encapsulation Again Inheritance Encapsulation and Inheritance The Object Superclass Flat vs. Nested
More informationCOE318 Lecture Notes Week 5 (Oct 3, 2011)
COE318 Software Systems Lecture Notes: Week 5 1 of 6 COE318 Lecture Notes Week 5 (Oct 3, 2011) Topics Announcements Strings static and final qualifiers Stack and Heap details Announcements Quiz: Today!
More informationAbstract class & Interface
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2124) Lab 3 Abstract class & Interface Eng. Mohammed Abdualal Abstract class 1. An abstract
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationInformatik II (D-ITET) Tutorial 6
Informatik II (D-ITET) Tutorial 6 TA: Marian George, E-mail: marian.george@inf.ethz.ch Distributed Systems Group, ETH Zürich Exercise Sheet 5: Solutions and Remarks Variables & Methods beginwithlowercase,
More informationMore Relationships Between Classes
More Relationships Between Classes Inheritance: passing down states and behaviors from the parents to their children Interfaces: grouping the methods, which belongs to some classes, as an interface to
More informationCSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community
CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationClasses and Inheritance Extending Classes, Chapter 5.2
Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.
More informationExample: Count of Points
Example: Count of Points 1 public class Point { 2... 3 private static int numofpoints = 0; 4 5 public Point() { 6 numofpoints++; 7 } 8 9 public Point(int x, int y) { 10 this(); // calling Line 5 11 this.x
More informationCS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals
CS/ENGRD 2110 FALL 2018 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110 Overview references in 2 Quick look at arrays: array Casting among classes cast, object-casting
More informationInheritance. Notes Chapter 6 and AJ Chapters 7 and 8
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1 Inheritance you know a lot about an object by knowing its class for example what is a Komondor? http://en.wikipedia.org/wiki/file:komondor_delvin.jpg
More informationSTUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes
STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes Java Curriculum for AP Computer Science, Student Lesson A20 1 STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes INTRODUCTION:
More informationWeek 5-1: ADT Design
Week 5-1: ADT Design Part1. ADT Design Define as class. Every obejects are allocated in heap space. Encapsulation : Data representation + Operation Information Hiding : Object's representation part hides,
More informationCOMP 250. Lecture 32. polymorphism. Nov. 25, 2016
COMP 250 Lecture 32 polymorphism Nov. 25, 2016 1 Recall example from lecture 30 class String serialnumber Person owner void bark() {print woof } : my = new (); my.bark();?????? extends extends class void
More informationJava Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.
Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either
More informationGoals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I
CSE1030 Introduction to Computer Science II Lecture #9 Inheritance I Goals for Today Today we start discussing Inheritance (continued next lecture too) This is an important fundamental feature of Object
More informationHAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.
HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,
More informationThe software crisis. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:
More informationPractice for Chapter 11
Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing
More informationInformatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,
Informatik II Tutorial 6 Mihai Bâce mihai.bace@inf.ethz.ch 05.04.2017 Mihai Bâce April 5, 2017 1 Overview Debriefing Exercise 5 Briefing Exercise 6 Mihai Bâce April 5, 2017 2 U05 Some Hints Variables &
More informationIntroduction to Computation and Problem Solving
Class 13: Inheritance and Interfaces Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward 2 More on Abstract Classes Classes can be very general at the top of
More informationLecture 4: Extending Classes. Concept
Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class s methods and fields, and
More informationGenerics method and class definitions which involve type parameters.
Contents Topic 07 - Generic Programming I. Introduction Example 1 User defined Generic Method: printtwice(t x) Example 2 User defined Generic Class: Pair Example 3 using java.util.arraylist II. Type
More informationCOMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code
COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,
More informationChapter 5 Object-Oriented Programming
Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares
More informationLecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson
Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson January 13, 2005 January 18, 2005 1 of 38 Lecture Goals Introduce the basic concepts of object-oriented analysis/design/programming
More informationCollections, Maps and Generics
Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.
More informationCOE318 Lecture Notes Week 13 (Nov 28, 2011)
COE318 Software Systems Lecture Notes: Week 13 1 of 8 COE318 Lecture Notes Week 13 (Nov 28, 2011) Topics Review Questions/Answers K. Clowes counselling hours for exam week Counselling Hours for Exam Week
More informationCS 11 java track: lecture 3
CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces
More informationInheritance and Interfaces
Inheritance and Interfaces what is inheritance? examples & Java API examples inheriting a method overriding a method polymorphism Object tostring interfaces Ex: sorting and Comparable interface Inheritance
More informationInheritance. The Java Platform Class Hierarchy
Inheritance In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those
More informationClass, Variable, Constructor, Object, Method Questions
Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx
More informationExample: Count of Points
Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;
More informationCS-202 Introduction to Object Oriented Programming
CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction
More informationName Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?
1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type
More informationInheritance. Transitivity
Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated
More informationCS Week 13. Jim Williams, PhD
CS 200 - Week 13 Jim Williams, PhD This Week 1. Team Lab: Instantiable Class 2. BP2 Strategy 3. Lecture: Classes as templates BP2 Strategy 1. M1: 2 of 3 milestone tests didn't require reading a file. 2.
More informationFundamental Java Methods
Object-oriented Programming Fundamental Java Methods Fundamental Java Methods These methods are frequently needed in Java classes. You can find a discussion of each one in Java textbooks, such as Big Java.
More informationOverview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008
Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object
More information1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:
1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code: public double getsalary() double basesalary = getsalary(); return basesalary + bonus; 3- What does the
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationOutline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.
Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not
More informationInheritance & Polymorphism. Object-Oriented Programming
Inheritance & Polymorphism Object-Oriented Programming Outline Example Design an inheritance structure IS-A and HAS-A Polymorphism protected access level Rules for overriding the Object class Readings:
More informationObject Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007
Object Fundamentals Part Three Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007 1 Lecture Goals Continue our tour of the basic concepts, terminology, and notations
More informationObject Oriented Programming. Java-Lecture 11 Polymorphism
Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class
More informationCOE318 Lecture Notes Week 3 (Sept 19, 2011)
COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Sept 19, 2011) Topics Announcements TurningRobot example PersonAge example Announcements The submit command now works. Quiz: Monday October
More informationThe class Object. Lecture CS1122 Summer 2008
The class Object http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Lecture 10 -- CS1122 Summer 2008 Review Object is at the top of every hierarchy. Every class in Java has an IS-A relationship
More informationQuestions Answer Key Questions Answer Key Questions Answer Key
Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (A) Name:. Status:
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationinterface MyAnno interface str( ) val( )
Unit 4 Annotations: basics of annotation-the Annotated element Interface. Using Default Values, Marker Annotations. Single-Member Annotations. The Built-In Annotations-Some Restrictions. 1 annotation Since
More informationtype conversion polymorphism (intro only) Class class
COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 1 Primitive Type Conversion double float long int short char byte boolean non-integers integers In COMP 273, you
More informationInformatik II Tutorial 6. Subho Shankar Basu
Informatik II Tutorial 6 Subho Shankar Basu subho.basu@inf.ethz.ch 06.04.2017 Overview Debriefing Exercise 5 Briefing Exercise 6 2 U05 Some Hints Variables & Methods beginwithlowercase, areverydescriptiveand
More information