0% found this document useful (0 votes)
23 views52 pages

Lecture 3

This document discusses inheritance and polymorphism in object-oriented programming. It defines inheritance as a process where a subclass inherits properties and behaviors from a superclass. Polymorphism allows subclasses to override or modify methods defined in the superclass. The document provides examples of defining subclasses, using the super keyword to call superclass constructors or methods, overriding methods, and constructor chaining where constructors are invoked along the inheritance chain.

Uploaded by

Waley Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views52 pages

Lecture 3

This document discusses inheritance and polymorphism in object-oriented programming. It defines inheritance as a process where a subclass inherits properties and behaviors from a superclass. Polymorphism allows subclasses to override or modify methods defined in the superclass. The document provides examples of defining subclasses, using the super keyword to call superclass constructors or methods, overriding methods, and constructor chaining where constructors are invoked along the inheritance chain.

Uploaded by

Waley Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

COMP 212 –

Programming II
2 - Inheritance and
Polymorphism(1)

1
Motivations

 Suppose you will define classes to model circles,


rectangles, and triangles. These classes have many
common features. What is the best way to design these
classes so to avoid redundancy?
 The answer is to use inheritance.
 Inheritance, polymorphism, and encapsulation
comprise the three central characteristics of object-
oriented programming.

2
2– Inheritance and Polymorphism-
Outline
Superclasses and Subclasses

Overriding methods

Using the super keyword

The Object Class

Polymorphism and Dynamic Binding

3
2– Inheritance and Polymorphism-
Outline (cont.)
The ArrayList Class

The instanceof Operator

The equals Method

The protected Data and Method

Casting Objects

4
Inheritance

 Inheritance is the process by which a new class (called


the derived class, child class or subclass) is created
from another class (called the base class, parent class
or superclass).
 Inheritance allows you to create class hierarchies – a
superclass gives its behavior and attributes to its
subclass. You are then free to modify or extend its
functionality.
 A subclass automatically has all the instance variables
and all the methods that the superclass has, and can
have additional methods and/or instance variables.

5
Inheritance

 The biggest advantage of Inheritance is that the code


that is already present in superclass need not be
rewritten in the subclass. (reuse code)
 Inheritance can also make application code more
intuitive/expressive

6
Superclasses and Subclasses
GeometricObject
-color: String The color of the object (default: white).
-filled: boolean Indicates whether the object is filled with a color (default: false).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled
filled: boolean) values.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
+toString(): String Returns a string representation of this object.

Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void 7
+printCircle(): void +getArea(): double
+getPerimeter(): double
Defining a Subclass

 A subclass inherits from a superclass. You can:


 Add new properties
 Add new methods
 Override the methods of the superclass

8
Using the Keyword super

 The keyword super refers to the superclass of the class


in which super appears. This keyword can be used in
two ways:
 To call a superclass constructor
 To call a superclass method

public void printCircle() {


System.out.println("The circle is created " +
super.getDateCreated() + " and the radius is " + radius);
}

9
Overriding Methods in the
Superclass
 A subclass inherits methods from a superclass.
Sometimes it is necessary for the subclass to modify the
implementation of a method defined in the superclass.
This is referred to as method overriding.
public class Circle extends GeometricObject {
// Other methods are omitted
/** Override the toString method defined in GeometricObject */
public String toString() {
return super.toString() + "\nradius is " + radius;
}
}

10
Overriding vs. Overloading
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }

class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }

class A extends B { class A extends B {


// This method overrides the method in B // This method overloads the method in B
public void p(double i) { public void p(int i) {
System.out.println(i); System.out.println(i);
} }
} }

11
Constructor between
Superclasses and Subclasses
 Are superclass’s Constructor Inherited?
 No. They are not inherited.
 They are invoked explicitly or implicitly.
 Explicitly using the super keyword.
 A constructor is used to construct an instance of a class.
Unlike properties and methods, a superclass's
constructors are not inherited in the subclass. They can
only be invoked from the subclasses' constructors, using
the keyword super.
 If the keyword super is not explicitly used, the
superclass's no-arg constructor is automatically
invoked.

12
The Object Class and Its
Methods
 Every class in Java is descended from the
java.lang.Object class. If no inheritance is specified
when a class is defined, the superclass of the class is
Object.

public class Circle { public class Circle extends Object {


... Equivalent
...
} }

13
Constructor between
Superclasses and Subclasses
 Superclass’s Constructor Is Always Invoked
 A constructor may invoke an overloaded constructor or
its superclass’s constructor. If none of them is invoked
explicitly, the compiler puts super() as the first
statement in the constructor.
 For example :
public A() { public A() {
is equivalent to
} super();
}

public A(double d) { public A(double d) {


// some statements is equivalent to
super();
} // some statements
}

14
Constructor between
Superclasses and Subclasses
 CAUTION : You must use the keyword super to call the
superclass constructor. Invoking a superclass
constructor’s name in a subclass causes a syntax error in
Java.
 Java requires that the statement that uses the keyword
super appear first in the constructor.

15
Constructor Chaining

 Constructing an instance of a class invokes all the


superclasses’ constructors along the inheritance chain.
This is called constructor chaining.
 Code Example :
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

... ... ...

16
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 1. Start from the
} main method
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 17
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 2. Invoke Faculty
} constructor
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 18
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
} 3. Invoke Employee’s no-
class Employee extends Person {
arg constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 19
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
4. Invoke Employee(String)
class Employee extends Person { constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 20
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
} 5. Invoke Person() constructor
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 21
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}
6. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 22
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}
7. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 23
Trace Code
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
9. Execute println
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 24
The Object Class and Its
Methods
 Every class in Java is descended from the
java.lang.Object class. If no inheritance is specified
when a class is defined, the superclass of the class is
Object.

public class Circle { public class Circle extends Object {


... Equivalent
...
} }

25
The toString() method in
Object Class
 The toString() method returns a string representation of
the object. The default implementation returns a string
consisting of a class name of which the object is an
instance, the at sign (@), and a number representing this
object.
 For example:
Loan loan = new Loan();
System.out.println(loan.toString());
 The example code displays something like Loan@15037e5 .
 Usually you should override the toString() method so that it
returns a digestible string representation of the object.

26
Polymorphism

 Polymorphism means that a variable of a supertype can


refer to a subtype object.
 A class defines a type. A type defined by a subclass is
called a subtype, and a type defined by its superclass is
called a supertype. Therefore, you can say that

Circle is a subtype of GeometricObject


and
GeometricObject is a supertype for Circle.

27
Polymorphism, Dynamic Binding
and Generic Programming
public class DynamicBindingDemo {
public static void main(String[] args) { Method m takes a parameter of the Object
m(new GraduateStudent()); type. You can invoke it with any object.
m(new Student());
m(new Person());
m(new Object());
}
 An object of a subtype can be used
wherever its supertype value is required.
public static void m(Object x) { This feature is known as polymorphism.
System.out.println(x.toString());
}  When the method m(Object x) is
} executed, the argument x’s toString()
class GraduateStudent extends Student { method is invoked. x may be an
} instance of GraduateStudent, Student,
Person, or Object. Classes
class Student extends Person {
GraduateStudent, Student, Person, and
public String toString() {
return "Student"; Object have their own implementation
} of the toString() method. Which
} implementation is used will be
class Person extends Object {
determined dynamically by the Java
public String toString() { Virtual Machine at runtime. This
return "Person"; capability is known as dynamic binding.
}
} 28
Dynamic Binding

 Dynamic binding works as follows: Suppose an object o is an instance of


classes C1, C2, ..., Cn-1, and Cn, where C1 is a subclass of C2, C2 is a
subclass of C3, ..., and Cn-1 is a subclass of Cn. That is, Cn is the most
general class, and C1 is the most specific class. In Java, Cn is the
Object class. If o invokes a method p, the JVM searches the
implementation for the method p in C1, C2, ..., Cn-1 and Cn, in this
order(bottom-top), until it is found. Once an implementation is found,
the search stops and the first-found implementation is invoked.

Cn Cn-1 ..... C2 C1

Since o is an instance of C1, o is also an


Object instance of C2, C3, …, Cn-1, and Cn
29
Method Matching vs. Binding

 Matching a method signature and binding a method


implementation are two issues.
 The compiler finds a matching method according to
parameter type, number of parameters, and order of
the parameters at compilation time.
 A method may be implemented in several subclasses.
The Java Virtual Machine dynamically binds the
implementation of the method at runtime.

30
Method Matching vs. Binding
(cont’d)
 Whether a method can be invoked literally on an instance
is a syntax issue – matching the method invocation to some
method signature
boolean isInClass = ……
GeometricObject b = isInClass? New Circle() : new Rectangle();
b.setColor(“Blue”);
whether b.setColor(“Blue”) is legal depends on if
GeometricObject has a method setColor(String)
 When a method can be invoked, which implementation to
select is a sematic issue – binding a particular
implementation according to the instance that the
reference actually points to at runtime
 whether to invoke the setColor in Circle or the setColor in
Rectangle depends on what kind of instance b points to

31
Generic Programming
public class DynamicBindingDemo {  Polymorphism allows methods to
public static void main(String[] args) {
m(new GraduateStudent()); be used generically for a wide
m(new Student()); range of object arguments. This is
m(new Person());
m(new Object()); known as generic programming.
}
 If a method’s parameter type is a
public static void m(Object x) {
System.out.println(x.toString());
superclass (e.g., Object), you may
} pass an object to this method of
}
any of the parameter’s subclasses
class GraduateStudent extends Student { (e.g., Student or String). When an
}
object (e.g., a Student object or a
class Student extends Person { String object) is used in the
public String toString() {
return "Student";
method, the particular
} implementation of the method of
}
the object that is invoked (e.g.,
class Person extends Object { toString()) is determined
public String toString() {
return "Person";
dynamically.
}
32
}
The ArrayList Class

 You can create an array to store objects. But the array’s size is fixed
once the array is created. Java provides the ArrayList class that can be
used to store an unlimited number of objects.

java.util.ArrayList<E>
+ArrayList() Creates an empty list.
+add(o: E) : void Appends a new element o at the end of this list.
+add(index: int, o: E) : void Adds a new element o at the specified index in this list.
+clear(): void Removes all the elements from this list.
+contains(o: Object): boolean Returns true if this list contains the element o.
+get(index: int) : E Returns the element from this list at the specified index.
+indexOf(o: Object) : int Returns the index of the first matching element in this list.
+isEmpty(): boolean Returns true if this list contains no elements.
+lastIndexOf(o: Object) : int Returns the index of the last matching element in this list.
+remove(o: Object): boolean Removes the element o from this list.
+size(): int Returns the number of elements in this list.
+remove(index: int) : boolean Removes the element at the specified index.
+set(index: int, o: E) : E Sets the element at the specified index.
33
Generic Type

 ArrayList is known as a generic class with a generic type


“E”. You can specify a concrete type to replace “E”
when creating an ArrayList. For example, the following
statement creates an ArrayList and assigns its reference
to variable cities. This ArrayList object can be used to
store strings.

ArrayList<String> cities = new ArrayList<String>();

34
Differences and Similarities
between Arrays and ArrayList

Operation Array ArrayList

Creating an array/ArrayList String[] a = new String[10] ArrayList<String> list = new ArrayList<>();


Accessing an element a[index] list.get(index);
Updating an element a[index] = "London"; list.set(index, "London");
Returning size a.length list.size();
Adding a new element list.add("London");
Inserting a new element list.add(index, "London");
Removing an element list.remove(index);
Removing an element list.remove(Object);
Removing all elements list.clear();

35
The instanceof Operator

 Use the instanceof operator to test whether an object is


an instance of a class:
Object myObject = new Circle();
... // Some lines of code
/** Perform casting if myObject is an instance of Circle */
if (myObject instanceof Circle) {
System.out.println("The circle diameter is " +
((Circle)myObject).getDiameter());
...
}

36
The equals Method

 The equals() method compares the


contents of two objects. The default implementation of the equals
method in the Object class is as follows:
public boolean equals(Object obj) {
return (this == obj);
}
 For example, the equals method is overridden in the Circle class.
public boolean equals(Object o) {
if (o instanceof Circle) {
return radius == ((Circle)o).radius;
}
else
return false;
}

37
The equals Method

 NOTE :The == comparison operator is used for


comparing two primitive data type values or for
determining whether two objects have the same
references.
 NOTE : The equals() method is intended to test whether
two objects have the same contents, provided that the
method is modified in the defining class of the objects.
 NOTE : The == operator is stronger than the equals
method, in that the == operator checks whether the
two reference variables refer to the same object.

38
The protected Modifier

 The protected modifier can be applied on data and


methods in a class. A protected data or a protected
method in a public class can be accessed by any class in
the same package or its subclasses, even if the
subclasses are in a different package.
 private, default, protected, public

Visibility increases

private, none (if no modifier is used), protected, public

39
Accessibility Summary

Modifier Accessed Accessed Accessed Accessed


on members from the from the from a from a different
in a class same class same package subclass package

public

protected -

default - -

private - - -

40
Visibility Modifiers
package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }

package p2;

public class C3 public class C4 public class C5 {


extends C1 { extends C1 { C1 o = new C1();
can access x; can access x; can access o.x;
can access y; can access y; cannot access o.y;
can access z; cannot access z; cannot access o.z;
cannot access u; cannot access u; cannot access o.u;
can invoke m(); can invoke m(); cannot invoke o.m();
} } }

41
The final Modifier

 The final class cannot be extended:


final class Math {
...
}
 The final variable is a constant:
final static double PI = 3.14159;
 The final method cannot be
overridden by its subclasses.

42
Casting Objects

 You might have already used the casting operator to convert


variables of one primitive type to another.
int intVal = (int)5.3;
 Casting can also be used to convert an object of one class type to
another within an inheritance hierarchy. In the preceding section,
the statement
m(new Student());
 assigns the object new Student() to a parameter of the Object type.
This statement is equivalent to:
Object o = new Student(); // Implicit casting
m(o);
The statement Object o = new Student(), known as implicit casting, is
legal because an instance of Student is automatically an instance of
Object.
43
Why Casting Is Necessary?

 Suppose you want to assign the object reference “o” to a variable


of the Student type using the following statement:
Object o = new Student();
Student b = o;
 A compile error would occur. Why does the statement Object o =
new Student() work and the statement Student b = o doesn’t?
This is because a Student object is always an instance of Object,
but an Object is not necessarily an instance of Student. Even
though you can see that “o” is really a Student object, the
compiler is not so clever to know it. To tell the compiler that “o” is
a Student object, use an explicit casting.
 The syntax is similar to the one used for casting among primitive
data types. Enclose the target object type in parentheses and place
it before the object to be cast, as follows:
Student b = (Student)o; // Explicit casting

44
Casting from
Superclass to Subclass
 Casting a reference does not change anything, especially not
the object pointed to by the reference. Roughly speaking, it
changes the class label of the object -- as if compiler invokes
the methods of the object according to the class label.
GeometricObject s = new Circle(); // s is created as a circle but now is labeled as GeometricObject

s.setRadius(1.0); // this is syntactically wrong, s is a now reference of GeometricObject

Circle c = (Circle) s; // downcasting must be explicit. But it can fail if s


// does not point to a Circle at runtime.

c.setRadius(1.0); // correct
s

GeometricObject
Shape

45
Casting from
Superclass to Subclass
 Explicit casting must be used when casting an object
from a superclass to a subclass. This type of casting
may not always succeed.
Apple x = (Apple)fruit;
Orange x = (Orange)fruit;
 An apple is a fruit, so you can always safely assign an
instance of Apple to a variable for Fruit. However, a
fruit is not necessarily an apple, so you have to use
explicit casting to assign an instance of Fruit to a
variable of Apple.
 However, the fruit (the instance) can be a orange.

46
Casting from
Superclass to Subclass
public class Animal {
public void callme() {
System.out.println("In callme of Animal");

 Note how the overridden }


}

methods are invoked public class Dog extends Animal {

after casting {
public void callme()

System.out.println("In callme of Dog");


}

public void callme2()


{
System.out.println("In callme2 of Dog");
}
}

public class testUpDownCast {


public static void main (String [] args) {
Dog d = new Dog();
((Animal)d).callme(); //Compiles, this invokes callme from Dog class.
((Animal)d).callme2(); //Compilation error

Animal d2 = new Dog();


d2.callme(); //Compiles, this invokes callme from Dog class.
d2.callme2(); //compilation error
((Dog)d2).callme2(); //Complies, invokes callme2 from Dog
} 47
}
Assignment 2
(Due date 30th Sept.)
 Basic Requirement:
 Write a JAVA program which ask for 4 user input: the date, the month, the year
and the event name (e.g. appointment/meeting name), and then output the
date and the event name onto the screen in a specific format.
 Submit your source code in the following name convention:
“YourName_YourStudentId_Assign2.java”
 Input:
 1. For the input of the day and the year, only number is acceptable.
 2. Range of input of the year to be accepted: from 1900 to 2100.
 3. For the input of the month, a number or an English word are acceptable.
 4. For the English word of the month input, full or 3 letters short form are
acceptable. uppercase or lowercase of each alphabet letter are acceptable.
(e.g. March, Mar, march, mar, mARCH, mAR or mArCH are all acceptable)
 5. If the input date is incorrect, user will be asked to re-input the date again.
The program will be terminated if it failed 3 times.
Hints: beware to the leap day.

48
Assignment 2
(Due date 30th Sept.)
 Output:
 1. If the input year is the year before (<) 2000, print the date as the following
format:
yyyy-mm-dd: (event name)
where the day(dd), the month(mm) and the year(yyyy) are printed with
numbers.
(e.g. 1999-03-24: meeting)

 2. Otherwise, print the date as the following format:


eee, dd mmm yyyy: (event name)
where eee is the English short form of the day of a week. The day(dd) and
year(yyyy) printed with numbers while month(mmm) printed with English short
form with capital letter form:
(e.g. Wed, 24 Mar 2021: Someone’s Birthday)

49
Assignment 2
(Due date 30th Sept.)
 Scoring (Total 12%):
 1. Correct and runnable program fulfills the Basic
Requirement (5%)
 2. Well structured code and good code style (1%)
 3. Suitable use of Overloading, String, Array (3%)
 4. Use Inheritance, polymorphism correctly (3%)

50
Assignment 2
(Due date 30th Sept.)
 To determine whether a year is a leap year, you can
follow these steps:
 1. If the year is evenly divisible by 4, go to step 2.
Otherwise, go to step 5.
 2. If the year is evenly divisible by 100, go to step 3.
Otherwise, go to step 4.
 3. If the year is evenly divisible by 400, go to step 4.
Otherwise, go to step 5.
 4. The year is a leap year (it has 366 days).
 5. The year is not a leap year (it has 365 days).

51
Assignment 2
(Due date 30th Sept.)
 The following is an example to calculate the day of a week:
 1. Take the last two digits of the year.
 2. Divide by 4, discarding any fraction.
 3. Add the day of the month.
 4. Add the month's key value: JFM AMJ JAS OND = 144 025 036
146
 5. Subtract 1 for January or February of a leap year.
 6. For a Gregorian date, add 6 for 2000's.
 7. Add the last two digits of the year.
 8. Divide by 7 and take the remainder.
 Resulting in 1 is Sunday, the first day of the week, 2 is Monday,
and so on.

52

You might also like