Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile."— Presentation transcript:

1 220 FINAL TEST REVIEW SESSION Omar Abdelwahab

2 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

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

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

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

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

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

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

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

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

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

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

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

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

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

16 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?

17 INHERITANCE AND POLYMORPHISM (CONTINUED) Demo, Sub, User Which classes can access b directly?

18 INHERITANCE AND POLYMORPHISM (CONTINUED) Demo Which classes can access c directly?

19 INHERITANCE AND POLYMORPHISM (CONTINUED) Demo, Sub

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

21 INHERITANCE AND POLYMORPHISM (CONTINUED) FALSE TRUE FALSE. Interfaces don't have data members and everything in an interface is static.

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

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

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

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

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

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

28 EXCEPTIONS(CONTINUED) What will be the output of the program?

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

30 EXCEPTIONS(CONTINUED)

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

32 Questions?

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


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

Similar presentations


Ads by Google