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

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
4/29/20151 Java Exceptions. 4/29/20152 Overview An exception is an unusual circumstance, such as an error condition, that must be handled in a non-standard.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
BY:- TOPS Technologies
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Interface, Subclass, and Abstract Class Review
Final and Abstract Classes
Inheritance and Polymorphism
Multithreading in Java
UML Class Diagram: class Rectangle
METHOD OVERRIDING in JAVA
Advanced Programming in Java
Lecture 11 Objectives Learn what an exception is.
CMSC 202 Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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)

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