220 Final Test Review Session

Download Report

Transcript 220 Final Test Review Session

220 FINAL TEST REVIEW SESSION
Omar Abdelwahab
INHERITANCE AND POLYMORPHISM
Suppose you have a class FunClass with public methods show, tell, and smile and
private methods get and put . If MoreFunClass is a subclass of FunClass, then write
the Java statement that creates this subclass by inheritance
INHERITANCE AND POLYMORPHISM (CONTINUED)
class MoreFunClass extends FunClass { }
INHERITANCE AND POLYMORPHISM (CONTINUED)
List the methods that MoreFunClass has (explain why each one is in MoreFunClass)
INHERITANCE AND POLYMORPHISM (CONTINUED)
show, tell, smile: these methods are inherited and accessible from FunClass because
MoreFunClass extends FunClass, and because these methods are public in FunClass.
MoreFunClass also includes all the methods of java.lang.Object, because all classes
extend Object.
INHERITANCE AND POLYMORPHISM (CONTINUED)
What kind of class cannot be instantiated as an object? Why?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Abstract classes cannot be instantiated because they may have one or more methods
that lack an implementation.
INHERITANCE AND POLYMORPHISM (CONTINUED)
What is the highest-level superclass in the Java language? What does this mean?
INHERITANCE AND POLYMORPHISM (CONTINUED)
java.lang.Object. This means that all instances of all classes inherit the methods of
Object, and can be stored in an Object reference.
INHERITANCE AND POLYMORPHISM (CONTINUED)
What is a constructor and how is it used in Java?
INHERITANCE AND POLYMORPHISM (CONTINUED)
A constructor is a method that is used to create a new instance of a class. The
constructor of FooClass is invoked by "new FooClass(…args…)"
INHERITANCE AND POLYMORPHISM (CONTINUED)
Explain the primary mechanism for code re-use in object-oriented programming?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Inheritance provides code re-use because it allows all specializations of a class to
share a single implementation of methods which are defined in the superclass.
INHERITANCE AND POLYMORPHISM (CONTINUED)
What is an interface in Java, and how is it used?
INHERITANCE AND POLYMORPHISM (CONTINUED)
An interface is a list of methods that a class which implements the interface must
implement. Classes may implement multiple interfaces. This is useful because it allows
an object to be stored in a variable whose type is any of the interfaces it implements,
which is typically used for message passing and event handling (callbacks).
INHERITANCE AND POLYMORPHISM (CONTINUED)
Suppose class Demo has three member data: a, which is public; b, which is private;
and c, which is protected. Class Sub is a subclass of Demo and class User has
instances of Demo as data members.
Which classes can access a directly?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Demo, Sub, User
Which classes can access b directly?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Demo
Which classes can access c directly?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Demo, Sub
INHERITANCE AND POLYMORPHISM (CONTINUED)
Abstract classes can be instantiated. (T/F)?
All members of an interface are public. (T/F)?
It's possible to define a non-static data member of an interface. (T/F)
INHERITANCE AND POLYMORPHISM (CONTINUED)
FALSE
TRUE
FALSE. Interfaces don't have data members and everything in an interface is static.
INHERITANCE AND POLYMORPHISM (CONTINUED)
What does it mean for a class member variable or method to be static? Give and
explain simple concrete code examples that illustrate the difference in how we would
access (public) static methods and non-static methods in other classes.
INHERITANCE AND POLYMORPHISM (CONTINUED)
Static members are independent of instances of the class, so static data members
have the same value for all instances of the class. We access static methods this way:
ClassName.staticMethodName(). We access non-static methods this way:
objectName.nonStaticMethodName().
INHERITANCE AND POLYMORPHISM (CONTINUED)
What is meant by the term multiple inheritance?
INHERITANCE AND POLYMORPHISM (CONTINUED)
Multiple inheritance occurs when a class has more than one superclass. This is
disallowed in Java. In Java, any class (concrete or abstract) can extend exactly one
(concrete or abstract) class, but implement one or more interfaces.
EXCEPTIONS
What will be the output of the program?
Public class foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println(“finally”);
}
}
}
EXCEPTIONS(CONTINUED)
you put a finally block after a try and its associated catch blocks, then once
execution enters the try block, the code in that finally block will definitely be
executed except in the following circumstances:
An exception arising in the finally block itself.
The death of the thread.
The use of System.exit()
Turning off the power to the CPU.
EXCEPTIONS(CONTINUED)
What will be the output of the program?
EXCEPTIONS(CONTINUED)
Compilation fails because ArithmeticException has already been
caught.ArithmeticException is a subclass of java.lang.Exception, by time
theArithmeticException has been specified it has already been caught by
theException class.
If ArithmeticException appears before Exception, then the file will compile. When
catching exceptions the more specific exceptions must be listed before the more
general (the subclasses must be caught before the superclasses).
EXCEPTIONS(CONTINUED)
EXCEPTIONS(CONTINUED)
Error is thrown but not recognised line(22) because the only catch attempts to catch
an Exception and Exception is not a superclass of Error. Therefore only the code in
the finally statement can be run before exiting with a runtime error (Exception in
thread "main" java.lang.Error).
Questions?
REFERENCES
https://www.cise.ufl.edu/