程序代写代做代考 go Java graph html COMP 250

COMP 250
INTRODUCTION TO COMPUTER SCIENCE
Week 4-1: OOD4 UML Diagrams and Inheritance
Giulia Alberini, Fall 2020

WHAT ARE WE GOING TO DO IN THIS VIDEO?
OOD4
UML diagrams  Inheritance

UML DIAGRAMS

UML DIAGRAMS
Unified Modeling Language (UML) provides a set of standard diagrams for graphically depicting object-oriented systems.
Class name goes here
Attributes/Fields are here Methods are here

EXAMPLE – DOG CLASS
 Fields/Attributes String name
Person owner
 Constructors
Dog(String name)
 Dog(String name, Person owner)
 Accessors and Mutators getName
getOwner  setName  setOwner
 Other Methods  eat()
 bark()  hunt()

DOG CLASS: + MEANS PUBLIC, – MEANS PRIVATE
Dog
– name : String
– owner : Person
<< constructors >>
+ Dog(name: String)
+ Dog(name: String, owner: Person) <>
+ getName() : String
+ getOwner() : Person <>
+ setName(String name)
+ setOwner(Person owner) <>
+ eat()
+ bark(int numOfTimes)
+ hunt(): Rabbit

UNDERLINE IF FIELD/METHOD IS STATIC
Dog
– name : String
– owner : Person
– numOfDogs: int
<< constructors >>
+ Dog(name: String)
+ Dog(name: String, owner: Person) <>
+ getName() : String
+ getOwner() : Person
+ getNumOfDogs(): int <>
+ setName(String name)
+ setOwner(Person owner) <>
+ eat()
+ bark(int numOfTimes)
+ hunt(): Rabbit

EASILY MAKE YOUR OWN DIAGRAMS
github.com/prmr/JetUML

INHERITANCE

THE DOG CLASS
 Throughout the next few lectures I’ll often refer to a Dog class.
public class Dog { private String name; private Person owner;
public Dog(String name) { this.name = name;
} }

EXAMPLES USING DOG
public class Dog {
private String name;
private Person owner;
public Dog(String aName) {
this.name = aName;
}
public static void main(String[] args) {
Dog myDog = new Dog(“Snoopy”);
System.out.println(myDog);
} }
 What prints?
 Dog@4aeda9d5

EXAMPLES USING DOG
public class Dog {
private String name;
private Person owner;
public Dog(String aName) { this.name = aName;
}
public static void main(String[] args) { Dog myDog = new Dog(“Snoopy”);
String s = myDog.toString(); System.out.println(s);
} }
 What prints?
 Dog@4aeda9d5

EXAMPLES USING DOG
public class Dog {
private String name;
private Person owner;
public Dog(String aName) { this.name = aName;
}
public static void main(String[] args) { Dog myDog = new Dog(“Snoopy”);
Dog aDog = myDog; System.out.println(myDog.equals(aDog));
} }
 What prints?  true

EXAMPLES USING DOG
public class Dog {
private String name;
private Person owner;
public Dog(String aName) { this.name = aName;
}
public static void main(String[] args) { Dog myDog = new Dog(“Snoopy”);
Dog aDog = new Dog(“Snoopy”); System.out.println(myDog.equals(aDog));
} }
 What prints?  false

toString() AND equals()
We have not defined these methods in the Dog class…  Where do they come from?
 Why can we use them?
Can we change what they do?

INHERITANCE
 In java, classes can be derived from other classes.
A class that is derived from another class is called a subclass.
 The class from which the subclass is derived is called a superclass.
A subclass inherits all public (or protected) fields and methods from its superclass. Constructors are the only thing that a subclass does not inherit.

BASIC IDEA
Suppose that you want to create a new class and that there is already a class that includes some of the code you want. Then instead of implementing this code, you can derive your new class from the existing one. By doing this, you can reuse the code from the existing class without having to write it and debug it again.

THE Object CLASS
 Object is the only class in java without a superclass. All other classes have
one and only one direct superclass.
 In the absence of any other specific superclass, every class is implicitly a
subclass of Object.
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

JAVA CLASS HIERARCHY
Object defines and implement methods common to all classes, including the ones you have been writing.

METHODS FROM Object
This is where equals and toString come from!!
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

AN EXAMPLE
Suppose we want to write a program with 3 classes: Animal, Dog, and Beagle.
All dogs are animals. All beagles are dogs.
Animals have a birthdate. Dogs bark.
Beagles chase rabbits.
relationships between classes
class definitions

AN EXAMPLE
Suppose the class Animal is implemented as follows:
public class Animal {
private Date birth;
public void eat(){ System.out.println(“Nom, nom, nom.”);
}

}

AN EXAMPLE
Then, we can declare a class Dog that is a subclass of Animal as follow:
public class Dog extends Animal { private Person owner;
public void bark(){
System.out.println(“Woof!”);
} }
 Dog inherits the method eat from Animal. It does not inherit the field birth because it is private. Dog also adds the field owner and the method bark.

A BIGGER PICTURE
Animal – birth: Date
+ eat() :
extends
Dog
– owner: Person
+ bark() :
extends
Beagle
+ hunt(): Rabbit
:

AS MANY SUBCLASSES AS WE NEED
Dog – owner: Person
+ bark() :
extends extends extends
Poodle + show()
:
Beagle
+ hunt(): Rabbit
:
Poodle, Beagle, and Doberman are all a subclasses of Dog. Dog is their superclass.
Doberman + fight()
:

TRY IT!
Let’s take a moment to create the Shape and Circle class and play around with methods and fields.
Shape – color: String
+ getColor(): String + setColor(c:String)
Circle
– radius: double
+ getRadius(): double + getArea(): double

WHAT CAN YOU DO IN A SUBCLASS?
A subclass inherits all the non-private fields and methods of its superclass. In the subclass you can use the inherited members as is, replace them, or hide them. You can also add new members.
 Fields:
 The inherited fields can be used as any other field.
 What if in the subclass you declare a field with the same name as the one in the superclass? Then you hide the inherited field.
(you should NOT do this)
 You can declare new field.

WHAT CAN YOU DO IN A SUBCLASS?
 Methods:
 The inherited methods can be used as they are.
If you write a non-static method with the same signature (and same return type) as the one from the superclass, you are overriding the method.
If you write a static method with the same signature (and same return type) as the one from the superclass, you are hiding the method.
 You can declare new methods in the subclass.
method signature = method name + list of
parameters.

OVERLOADING VS OVERRIDING
OVERLOADING
 Two or more methods in the same class with same name but different parameters. (i.e. different signature)
OVERRIDING
 Two (instance) methods with same signature and return type, one in the parent class, one in the child class.

EXAMPLES – OVERLOADING
The method abs from Math is The methods add and remove from overloaded ArrayList are overloaded.
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

EXAMPLES – OVERLOADING
Dog
– owner : Person
public void { print(“woof!”);
}
:
bark()
Different signature (= name, ≠ parameters)
extends
Beagle + hunt()
public void { for(int i=0; i