33
220 FINAL TEST REVIEW SESSION Omar Abdelwahab

220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

Embed Size (px)

Citation preview

Page 1: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

220 FINAL TEST REVIEW SESSION

Omar Abdelwahab

Page 2: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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

Page 3: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) class MoreFunClass extends FunClass { }

Page 4: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) List the methods that MoreFunClass has (explain why each one is in MoreFunClass)

Page 5: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 6: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) What kind of class cannot be instantiated as an object? Why?

Page 7: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) Abstract classes cannot be instantiated because they may have one or more methods that lack an implementation.

Page 8: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) What is the highest-level superclass in the Java language? What does this mean?

Page 9: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 10: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) What is a constructor and how is it used in Java?

Page 11: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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…)"

Page 12: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) Explain the primary mechanism for code re-use in object-oriented programming?

Page 13: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 14: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) What is an interface in Java, and how is it used?

Page 15: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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).

Page 16: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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?

Page 17: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) Demo, Sub, User

Which classes can access b directly?

Page 18: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) Demo

Which classes can access c directly?

Page 19: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) Demo, Sub

Page 20: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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)

Page 21: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) FALSE

TRUE

FALSE. Interfaces don't have data members and everything in an interface is static.

Page 22: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 23: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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().

Page 24: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

INHERITANCE AND POLYMORPHISM (CONTINUED) What is meant by the term multiple inheritance?

Page 25: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 26: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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”);

}

}

}

Page 27: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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.

Page 28: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

EXCEPTIONS(CONTINUED)

What will be the output of the program?

Page 29: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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).

Page 30: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

EXCEPTIONS(CONTINUED)

Page 31: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

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).

Page 32: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

Questions?

Page 33: 220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile

REFERENCES

https://www.cise.ufl.edu/