Assignment 01
1.
(i) Inheritance: inheritance (in Java) is when a class extends another class, and consequently includes all
the visible methods and variables from the other class as part of its own interface.
(ii) Polymorphism: the ability to create a variable, object, function, operator, or object that has more
than one form; also defined as the ability for values of different data types to be handled using a
uniform interface.
(iii) Superclass: the parent of a subclass. Provides functionality, in the form of methods and variables,
that are inherited by a subclass.
(iv) Subclass: a class which extends a superclass. Inherits functionality, in the form of methods and
variables, from the superclass.
(v) Abstract Class: a class which declares an abstract method – a method that lacks an implementation
(body). Abstract classes cannot be implemented directly. They must be first extended by subclasses,
which provide implementations of the abstract methods.
2.
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in
its superclass, then the method in the subclass is said to override the method in the superclass, and this
process is called method overriding.
On the other hand, when multiple methods have the same name, but different type signatures, then the
methods are said to be overloaded, and this process is called method overloading.
Rules associated with method overloading:
1. Overloaded methods must differ in the type and/or number of their parameters.
2. Return types do not play a role in overload resolution.
Rule associated with method overriding:
Overridden methods must match in the type and number of their parameters as well as their return
types.
3.
a. Instance variable and Class variable
Instance Variables (Non-Static Fields): An instance variable is any field declared without the static
modifier. It is called such because its value is unique to each instance of a class (or object).
Class Variables (Static Fields): A class variable is any field declared with the static modifier; this tells
the compiler that there is exactly one copy of this variable in existence, regardless of how many times
the class has been instantiated.
1
b. If statement and Switch statement
A switch statement differs from the if in that switch can only test for equality, whereas if can evaluate
any type of boolean expressions. the switch statement is more efficient than the if statement.
c. Implementation and Extend
The difference between an interface and a regular class is that in an interface you can not specify
an specific implementation (only its "interface"). More specific, this means you can only specify
methods, but not implement them.
Also java doesn't support multiple inheritance for classes. This is solved by using multiple
interfaces.
4.
2
5.
or
Rectangle
3
Triangle
6. Circle – display() method
4
Triangle – display() method
Rectangle – display() method
5
7.
Shape
Triangle Circle Rectangle
8.
6
9.