COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan

Size: px
Start display at page:

Download "COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan"

Transcription

1 COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS Instructor: Prasun Dewan

2 A POINTHISTORY IMPLEMENTATION public class APointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() {return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = new ACartesianPoint(x, y); contents[size] = p; size++; 2

3 A POLARPOINTHISTORY IMPLEMENTATION public class APolarPointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; Reducing code duplication? protected boolean isfull() {return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = new APolarPoint(x, y); contents[size] = p; size++; 3

4 A POLARPOINTHISTORY IMPLEMENTATION public class APolarPointHistoryWithAddMethod extends APointHistory { public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = new APolarPoint(x, y); contents[size] = p; size++; Only line changed, still code duplication 4

5 A POINTHISTORY IMPLEMENTATION public class ACartesianPointHistoryWithDynamicallyDispatchedMethod implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; protected Point createpoint(int x, int y) { return new ACartesianPoint(x, y); 5

6 OVERRIDING METHOD public class APolarPointHistoryWithDynamicallyDispatchedMethod extends ACartesianPointHistoryWithDynamicallyDispatchedMethod { protected Point createpoint(int x, int y) { return new ACartesianPoint(x, y); public static void main (String[] args) { PointHistory pointhistory = new APolarPointHistoryWithDynamicallyDispatchedMethod(); pointhistory.addelement(50, 100); 6

7 A POINTHISTORY IMPLEMENTATION public class ACartesianPointHistoryWithDynamicallyDispatchedMethod implements PointHistory { public public class final APolarPointHistoryWithDynamicallyDispatchedMethod MAX_SIZE = 50; protected extends Point[] ACartesianPointHistoryWithDynamicallyDispatchedMethod contents = new Point[MAX_SIZE]; { protected protected int Point size createpoint(int = 0; x, int y) { public return int new size() ACartesianPoint(x, { y); return size; public Point elementat (int index) { return contents[index]; PointHistory pointhistory = new APolarPointHistoryWithDynamicallyDispatchedMethod(); protected boolean isfull() { pointhistory.addelement(50, 100); return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; protected Point createpoint(int x, int y) { return new ACartesianPoint(x, y); Static dispatching: call most specific method known when caller method was compiled Dynamic dispatching: call most specific method for the calling object 7

8 VIRTUAL VS. REGULAR METHOD AND DIFFERENT LANGUAGES Virtual method: Call to it is dynamically dispatched Regular method: Call to it is statically dispatched Java: All instance methods are virtual methods and all class (static)methods are regular methods C++: Instance methods can be declared to be regular or virtual and all class (static)methods are regular methods Smalltalk: All methods are virtual 8

9 INHERITANCE RELATIONSHIPS? APolarPointHistoryWithDynamicallyDispatchedMethod IS-A ACartesianPointHistoryWithDynamicallyDispatchedMethod ACartesianPointHistoryWithDynamicallyDispatchedMethod IS-A APolarPointHistoryWithDynamicallyDispatchedMethod 9

10 CORRECT INHERITANCE AnAbstractPointHistory IS-A ACartesianPointHistory IS-A APolarPointHistory 10

11 CORRECT INHERITANCE AnAbstractPointHistory IS-A ACartesianPointHistory IS-A APolarPointHistory 11

12 A CATERSIAN HISTORY IMPLEMENTATION public class ACartesianPointHistoryWithDynamicallyDispatchedMethod implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; Changed how? protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; protected Point createpoint(int x, int y) { return new ACartesianPoint(x, y); 12

13 REMOVING CREATEPOINT public abstract class AnAbstractPointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; 13

14 NULL OVERRIDDEN METHOD public abstract class AnAbstractPointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; protected Point createpoint(int x, int y) { 14

15 OVERRIDING DYNAMICALLY DISPATCHED METHOD public class ACartesianPointHistory extends AnAbstractPointHistory{ protected Point createpoint(int x, int y) { return new ACartesianPoint(x, y); 15

16 ERRONEOUS IMPLEMENTATION WITH NO ERRORS public class ACartesianPointHistory extends AnAbstractPointHistory{ 16

17 ABSTRACT METHOD public abstract class AnAbstractPointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; Only header of method provided as in interface protected abstract Point createpoint(int x, int y); 17

18 ERRONEOUS IMPLEMENTATION public class ACartesianPointHistory extends AnAbstractPointHistory{ Java complains abstract method not implemented in concrete class 18

19 NULL VS. ABSTRACT METHODS Null methods could replace abstract methods. Abstract method force overriding implementations in subclasses. Provide better documentation, indicating to programmer that subclass will implement them. 19

20 ABSTRACT METHOD Declared only in abstract classes Keyword: abstract No body Each (direct or indirect) subclass must implement abstract methods defined by an abstract class. Much like each class must implement the methods defined by its interface(s). 20

21 ABSTRACT CLASS VS. METHODS Abstract method => containing class abstract Cannot have an unimplemented method in an instance Abstract class may not contain abstract method 21

22 A COURSE WITH ABSTRACT METHOD public abstract class ACourse { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); public String tostring() { return "Title:" + title + " Dept:" + dept + " Number: " + getnumber(); Abstract Method 22

23 EQUIVALENT WITH INTERFACE public abstract class ACourse implements Course { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; public String tostring() { return "Title:" + title + " Dept:" + dept + " Number: " + getnumber(); An abstract class can implement any interface This means all sub classes implement the interface Interface implementation check is made for concrete classes 23

24 ANOTHER COURSE package courses; public abstract class ACourse implements Course { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; public String tostring() { return "Title:" + title + " Dept:" + dept + " Number: " + getnumber(); This means all sub Interface implementation An abstract class can classes implement the check is made for concrete implement any interface classes interface 24

25 ALTERNATIVE AREGULARCOURSE public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public int getnumber() { return coursenum; Saying ARegularCourse implements Course is redundant 25

26 ALTERNATIVE AFRESHMANSEMINAR public class AFreshmanSeminar extends ACourse { public AFreshmanSeminar (String thetitle, String thedept) { super (thetitle, thedept); public int getnumber() { return SEMINAR_NUMBER; No need for implementation clause 26

27 WHEN ABSTRACT METHOD REQUIRED public abstract class AnAbstractPointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; protected abstract Point createpoint(int x, int y); Not public 27

28 ADDING CREATEPOINT TO INTERFACE public interface PointHistoryWithExtraPublicMethod { public Point createpoint(int x, int y); Factory method public abstract class AnAbstractPointHistoryWithExtraPublicMethod implements PointHistoryWithExtraPublicMethod { public final int MAX_SIZE = 50; protected Point[] contents = new Point[MAX_SIZE]; protected int size = 0; public int size() { return size; public Point elementat (int index) { Least privilege violated return contents[index]; protected boolean isfull() { return size == MAX_SIZE; public void addelement(int x, int y) { if (isfull()) System.out.println("Adding item to a full history"); else { Point p = createpoint(x, y); contents[size] = p; size++; 28

29 FACTORY ABSTRACT METHOD abstract <T> <M>( ) ; extends <T> <M>( ) { return new <C1> ( ) <T> <M> ( ){ return new <C2> ( ) Abstract method that returns an instance of some type T like a factory, it creates and initializes an object. 29

30 ABSTRACT METHODS VS. INTERFACES Unimplemented methods become abstract methods to be implemented by concrete subclasses. Do not need explicit implements clause in subclass if no additional public methods implemented, public class ARegularCourse extends ACourse {. A class with only abstract methods simulates interfaces. No multiple inheritance in Java Separation of abstract and concrete methods with interfaces. A class implements but does not inherit a specification! 30

31 ABSTRACT METHODS VS. INTERFACES Non-public abstract methods relevant even in Java All interface methods must be public. May want non public abstract methods.e.g. abstract boolean isfull() ; 31

32 ABSTRACT METHODS VS. VIRTUAL METHODS Abstract methods must be virtual methods. Virtual methods need not be abstract methods 32

33 FACTORY METHODS VS. ABSTRACT METHODS Some abstract methods may be factory methods Factory methods may not be abstract methods but often this means bad programming practice APolarPointHistoryWithDynamicallyDispatchedMethod IS-A ACartesianPointHistoryWithDynamicallyDispatchedMethod 33

34 OVERLOADING, POLYMORPHISM, AND DYNAMIC DISPATCH Polymorphic method ARegularCourse static void print (Course course) { System.out.println( course.gettitle() + " " + course.getdepartment() + course.getnumber() ); AFreshmanSeminar getnumber() in ARegularCouse static void print (CourseList courselist) { Overloaded method print(course); getnumber() in AFreshmanSeminar Dynamically dispatched method 34

35 OVERLOADING, POLYMORPHISM, AND DYNAMIC DISPATCH Same method name works on different types. Overloading Multiple (static or instance) methods with same name but different parameter types. Resolved at compile time based on parameter types Polymorphism Same method implementation works on multiple object types. Dynamic dispatch (or virtual methods) Multiple instance methods with same name in different classes Resolved at execution time based on type of target object 35

36 POLYMORPHISM VS. OVERLOADING AND DYNAMIC DISPATCH Polymorphism vs. Overloading Polymorphism: single print (Course course) Overloading: print (ARegularCourse course) and print (AFreshmanSeminar course) with same implementation. Polymorphism vs. Dynamic Dispatch Polymorphism: single gettitle() in ACourse Dynamic dispatch: gettitle() in AFreshmanSeminar() and gettitle() in ARegularCourse() with same implementation. Create polymorphic code when you can In overloading and dynamic dispatch, multiple implementations associated with each method name. In polymorphic case, single implementation. Use interfaces rather than classes as types of parameters Use supertypes rather than subtypes as types of parameters 36

37 POLYMORPHISM VS. OVERLOADING AND DYNAMIC DISPATCH Cannot always create polymorphic method. getnumber() for ARegularCourse and AFreshmanSeminar do different things. print(course course) and print (CourseList courselist) do different things. When polymorphism not possible try overloading and dynamic dispatch. 37

38 MANUAL INSTEAD OF DYNAMIC DISPATCH static void print (Course course) { if (course instanceof ARegularCourse) number = ((ARegularCourse)course). getcoursenumberregularcourse (); else if (course instanceof AFreshmanSeminar) number = ((AFreshmanSeminar)course). getcoursenumberfreshmanseminar (); System.out.println( course.gettitle() + " " + course.getdepartment() + number ); More Work Must update code as subtypes added 38

39 CAN WE GET RID OF MANUAL DISPATCH/INSTANCEOF? static void print (Course course) { if (course instanceof RegularCourse) System.out.print( Regular Course: ); else if (course instanceof AFreshmanSeminar) System.out.println( Freshman Seminar ); System.out.println( course.gettitle() + " " + course.getdepartment() + course.getnumber() ); 39

40 CANNOT ALWAYS GET RID OF MANUAL DISPATCH/INSTANCOF static void print (Course course) { printheader (course); System.out.println( course.gettitle() + " " + course.getdepartment() + course.getnumbert() ); static void printheader (ARegularCourse course) { System.out.print( Regular Course: ); static void printheader (AFreshmanSeminar course) { System.out.print( Freshman Seminar: ); At compile time, do not know if regular course or freshman seminar Actual parameter cannot be supertype of formal parameter 40

41 PRINTHEADER() IN AREGULARCOURSE package courses; public class ARegularCourse extends ACourse implements Course { public void printheader () { System.out.print ( Regular Course: ) 41

42 PRINTHEADER() IN AFRESHMANSEMINAR package courses; public class AFreshmanSeminar extends ACourse implements FreshmanSeminar { public void printheader () { System.out.print ( Freshman Seminar: ) 42

43 SHOULD NOT ALWAYS GET RID OF MANUAL DISPATCH/INSTANCOF static void print (Course course) { printheader (course); System.out.println( course.gettitle() + " " + course.getdepartment() + course.getnumbert() ); static void printheader (ARegularCourse course) { System.out.print( Regular Course: ); static void printheader (AFreshmanSeminar course) { Should not System.out.print( Freshman put Hence Seminar: need to use ); instanceof printheader() in ARegularCourse or AFreshmanSeminar as it is UI code. Used in parsers 43

44 EXTRA SLIDES 44

45 A STRINGHISTORY IMPLEMENTATION public class AStringHistory implements StringHistory { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; public void addelement(string element) { if (isfull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; 45

46 A BIG STRINGHISTORY IMPLEMENTATION public class ABigStringHistory implements StringHistory { public static final int MAX_SIZE = 500; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; public void addelement(string element) { if (isfull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; 46

47 NULL OVERRIDDEN METHOD public abstract class AnAbstractStringHistory { boolean isfull(){; void uncheckedaddelement(object element){; public void addelement(object element) { if (!isfull()) uncheckedaddelement(element); else handleerror(); void handleerror () { System.out.println("Adding item to a full history"); ; 47

48 A STRINGHISTORY IMPLEMENTATION public class AStringHistory extends AnAbstractStringHistory implements StringHistory { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; void uncheckedaddelement(string element) { contents[size] = element; size++; 48

49 DYNAMIC DISPATCH public abstract class ACollection { boolean isfull(){; void uncheckedaddelement(object element){; public void addelement(object element) { if (!isfull()) uncheckedaddelement(element); else public class AStringHistory handleerror(); extends ACollection implements StringHistory { void boolean handleerror isfull() { () return { size == MAX_SIZE; void uncheckedaddelement(string System.out.println("Adding element) item to { a full history"); ; contents[size] = element; size++; Which isfull() called when addelement is invoked on an instance of AStringHistory()? Regular method: called method statically dispatched at compile time, so overridden isfull() Virtual method: most specific implementation of called method dynamically dispatched at execution time, so overriding isfull() 49

50 REGULAR VS. VIRTUAL METHOD Smalltalk: had only virtual methods With each object a table kept which associates method header with most specific implementation. Table accessed at execution time. C++: Had both virtual and regular methods More efficient with regular methods Java: Has only virtual methods Abstract and interface method by definition are virtual methods 50

51 CORRECT A BIG STRINGHISTORY IMPLEMENTATION public class ABigStringHistory extends AnAbstractStringHistory implements StringHistory { public final int MAX_SIZE = 500; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; void uncheckedaddelement(string element) { contents[size] = element; size++; 51

52 AN ERRONEOUS BIG STRINGHISTORY IMPLEMENTATION public class ABigStringHistory extends AnAbstractStringHistory implements StringHistory { public final int MAX_SIZE = 500; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; //void uncheckedaddelement(string element) { // contents[size] = element; // size++; // 52

53 NULL OVERRIDDEN METHOD public abstract class AnAbstractStringHistory { boolean isfull(){; void uncheckedaddelement(object element){; public void addelement(object element) { if (!isfull()) uncheckedaddelement(element); else handleerror(); void handleerror () { System.out.println("Adding item to a full history"); ; 53

54 ABSTRACT METHODS public abstract class AnAbstractStringHistory { abstract boolean isfull(); abstract void uncheckedaddelement(object element) public void addelement(object element) { if (!isfull()) uncheckedaddelement(element); else handleerror(); void handleerror () { System.out.println("Adding item to a full history"); ; Only header of method provided as in interface 54

55 AN ERRONEOUS BIG STRINGHISTORY IMPLEMENTATION public class ABigStringHistory implements StringHistory { public final int MAX_SIZE = 500; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; public String elementat (int index) { return contents[index]; boolean isfull() { return size == MAX_SIZE; //void uncheckedaddelement(string element) { // contents[size] = element; // size++; // Java complains abstract method not implemented in concrete class 55

56 NULL VS. ABSTRACT METHODS public abstract class AnAbstractStringHistory { boolean isfull() { void uncheckedaddelement(object element) { public void addelement(object element) { if (!isfull()) uncheckedaddelement(element); else handleerror(); void handleerror () { System.out.println("Adding item to a full history"); ; Null methods could replace abstract methods in this example. Abstract method force overriding implementations in subclasses. Provide better documentation, indicating to programmer that subclass will implement them. 56

57 ACARTESIANPLANE CONSTRUCTOR public class ACartesianPlane implements CartesianPlane { int originx, originy; int axeslength; Line xaxis; Line yaxis; StringShape xlabel; StringShape ylabel; public ACartesianPlane (int theaxeslength, int theoriginx, int theoriginy ) { axeslength = theaxeslength; originx = theoriginx; originy = theoriginy; xaxis = new ALine(toXAxisX(), toxaxisy(), axeslength, 0); yaxis = new ALine(toYAxisX(), toyaxisy(), 0, axeslength); xlabel = new AStringShape ("X", toxlabelx(), toxlabely()); ylabel = new AStringShape ("Y", toylabelx(), toylabely()); 57

58 NOBSERVABLE CARTESIANPLANE CONSTRUCTOR 58

59 ACARTESIANPLANE 59

60 ANOBSERVABLE CARTESIANPLANE 60

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan COMP 401 INITIALIZATION AND INHERITANCE Instructor: Prasun Dewan PREREQUISITE Inheritance Abstract Classes. 2 MORE INHERITANCE 3 AREGULARCOURSE public class ARegularCourse extends ACourse implements Course

More information

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan COMP 401 ABSTRACT CLASSES Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TOPICS Top-Down vs. Bottom-Up Inheritance Abstract Classes 3 COURSE DISPLAYER User inputs course title Program displays course

More information

COMP 401 Prasun Dewan 1

COMP 401 Prasun Dewan 1 21. More Inheritance COMP 401 Prasun Dewan 1 In this chapter, we will go deeper into inheritance. We will distinguish between bottom up design of inheritance, which we did in the examples of chapter 17,

More information

COMP 401 Prasun Dewan 1

COMP 401 Prasun Dewan 1 21. More Inheritance COMP 401 Prasun Dewan 1 In this chapter, we will go deeper into inheritance. We will distinguish between bottom-up design of inheritance, which we did in the examples of chapter 17,

More information

Collections and Inheritance

Collections and Inheritance COMP 401 Prasun Dewan 1 Collections and Inheritance In the previous chapter, we defined logical structures with fixed number of nodes. In this chapter, we will see how we can use arrays to define types

More information

23. Generics and Adapters

23. Generics and Adapters COMP 401 Prasun Dewan 1 23. Generics and Adapters The term generic implies unspecialized/common behavior. In an object oriented language, it seems to apply to the class Object, which describes the behavior

More information

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan COMP 401 INHERITANCE: IS-A Instructor: Prasun Dewan PREREQUISITE Interfaces Inheritance and Arrays 2 IS-A IS-A Relationship Human IS-A Mammal Salmon IS-A Fish ACartesianPoint IS-A Point T1 IS-A T2 if T1

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 17. Inheritance COMP 110 Prasun Dewan 1 We have seen how we can create arbitrary sequences, such as histories, using arrays. In this chapter, we will use arrays to create several new kinds of types including

More information

Inheritance and Variables

Inheritance and Variables COMP 401 Prasun Dewan 1 Inheritance and Variables In the previous chapter, we studied the basics of inheritance in the context of collections. Here, we will use a different example to present subtleties

More information

COMP 401 INHERITANCE. Instructor: Prasun Dewan

COMP 401 INHERITANCE. Instructor: Prasun Dewan COMP 401 INHERITANCE Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 INHERITANCE Inheritance Inheriting ancestor s traits Inheriting benefactor s assets Inheriting instance members

More information

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 STATIC VS. DYNAMIC STRUCTURES Static Beans have fixed number of properties Arrays have fixed number

More information

COMP 401 GRAPHICS. Instructor: Prasun Dewan

COMP 401 GRAPHICS. Instructor: Prasun Dewan COMP 401 GRAPHICS Instructor: Prasun Dewan PREREQUISITE Interfaces 2 MORE ON OBJECTS Graphics types Test-first approach Stubs Physical vs. logical representation Representations with errors 3 MATHEMATICAL

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150, COMP 401: THE DUAL ROLE OF A CLASS Instructor: Prasun Dewan (FB 150, dewan@unc.edu) SCRIPTS ANALOGY Script Program Follows Follows Theater Performer 2 STRUCTURING IN SCRIPTS Script (Folder) Act (File)

More information

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Topic 5 Polymorphism.  Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code. Topic 5 Polymorphism " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code. 1 Polymorphism Another feature of OOP literally having many forms object variables in

More information

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 COLLECTION TYPES StringHistory, StringDatabase, StringSet Array ArrayList, List Map Stack Queue 3

More information

Binghamton University. CS-140 Fall Dynamic Types

Binghamton University. CS-140 Fall Dynamic Types Dynamic Types 1 Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

Inheritance. A key OOP concept

Inheritance. A key OOP concept Inheritance A key OOP concept Setting the scene Why do we need inheritance? Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

CLASS DESIGN. Objectives MODULE 4

CLASS DESIGN. Objectives MODULE 4 MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

CS Programming I: Inheritance

CS Programming I: Inheritance CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance

More information

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan PREREQUISITES Interfaces Main Console Input Inheritance 2 INTERACTIVE APP. VS. USER/PROG. INTERFACE public class ABMISpreadsheet implements

More information

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass? 1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004 Type Hierarchy Comp-303 : Programming Techniques Lecture 9 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 1 Last lecture...

More information

More about inheritance

More about inheritance Main concepts to be covered More about inheritance Exploring polymorphism method polymorphism static and dynamic type overriding dynamic method lookup protected access 4.1 The inheritance hierarchy Conflicting

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Inheritance. Transitivity

Inheritance. Transitivity Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated

More information

Lecture 4: Extending Classes. Concept

Lecture 4: Extending Classes. Concept Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class s methods and fields, and

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are built on top of that. CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object

More information

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name: CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides

More information

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School Class Hierarchy and Interfaces David Greenstein Monta Vista High School Inheritance Inheritance represents the IS-A relationship between objects. an object of a subclass IS-A(n) object of the superclass

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

Inheritance and object compatibility

Inheritance and object compatibility Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

CIS 110: Introduction to computer programming

CIS 110: Introduction to computer programming CIS 110: Introduction to computer programming Lecture 25 Inheritance and polymorphism ( 9) 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Inheritance Polymorphism Interfaces 12/3/2011

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan COMP 401 EXCEPTIONS Instructor: Prasun Dewan PREREQUISITE Inheritance Interfaces Input Iterator 2 EXCEPTIONS: WHAT? Exceptions have to do with custom error handling Errors Internal errors in a program

More information

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan COMP 401 INHERITANCE: TYPE CHECKING Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TYPE-CHECKING EXAMPLES StringHistory stringhistory = new AStringDatabase(); StringDatabase stringdatabase = new AStringHistory();

More information

Parametric Polymorphism for Java: A Reflective Approach

Parametric Polymorphism for Java: A Reflective Approach Parametric Polymorphism for Java: A Reflective Approach By Jose H. Solorzano and Suad Alagic Presented by Matt Miller February 20, 2003 Outline Motivation Key Contributions Background Parametric Polymorphism

More information

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Big software. code reuse: The practice of writing program code once and using it in many contexts. Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting

More information

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Inheritance -- Introduction

Inheritance -- Introduction Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Implements vs. Extends When Defining a Class

Implements vs. Extends When Defining a Class Implements vs. Extends When Defining a Class implements: Keyword followed by the name of an INTERFACE Interfaces only have method PROTOTYPES You CANNOT create on object of an interface type extends: Keyword

More information

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented Table of Contents L01 - Introduction L02 - Strings Some Examples Reserved Characters Operations Immutability Equality Wrappers and Primitives Boxing/Unboxing Boxing Unboxing Formatting L03 - Input and

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

Abstract Classes and Interfaces

Abstract Classes and Interfaces Abstract Classes and Interfaces Reading: Reges and Stepp: 9.5 9.6 CSC216: Programming Concepts Sarah Heckman 1 Abstract Classes A Java class that cannot be instantiated, but instead serves as a superclass

More information

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram. Jordan University Faculty of Engineering and Technology Department of Computer Engineering Object-Oriented Problem Solving: CPE 342 Lab-8 Eng. Asma Abdel Karim In this lab, you will be given the implementation

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 10 - Object-Oriented Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

Overview of Java s Support for Polymorphism

Overview of Java s Support for Polymorphism Overview of Java s Support for Polymorphism Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt

More information

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures Chapter 5: Procedural abstraction Proper procedures and function procedures Abstraction in programming enables distinction: What a program unit does How a program unit works This enables separation of

More information

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts. Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance

More information

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London Software and Programming I Classes and Arrays Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Class Object Interfaces Arrays Sections 9.5 9.6 Common Array Algorithms Sections 6.1

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan COMP 401 EXCEPTIONS Instructor: Prasun Dewan PREREQUISITE Inheritance Interfaces Input Iterator 2 EXCEPTIONS: WHAT? Exceptions have to do with error handling Run time Custom Error kinds Internal errors

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members

More information

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming

More information

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

Last class. -More on polymorphism -super -Introduction to interfaces

Last class. -More on polymorphism -super -Introduction to interfaces Last class -More on polymorphism -super -Introduction to interfaces Interfaces Sometimes in Java, we will have 2 classes that both share a similar structure, but neither of them is clearly the parent or

More information

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass Inheritance and Polymorphism Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism Inheritance (semantics) We now have two classes that do essentially the same thing The fields are exactly

More information

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B

More information

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

Inheritance (Part 5) Odds and ends

Inheritance (Part 5) Odds and ends Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there

More information

[ L5P1] Object-Oriented Programming: Advanced Concepts

[ L5P1] Object-Oriented Programming: Advanced Concepts [ L5P1] Object-Oriented Programming: Advanced Concepts Polymorphism Polymorphism is an important and useful concept in the object-oriented paradigm. Take the example of writing a payroll application for

More information

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments CMSC433, Spring 2004 Programming Language Technology and Paradigms Java Review Jeff Foster Feburary 3, 2004 Administrivia Reading: Liskov, ch 4, optional Eckel, ch 8, 9 Project 1 posted Part 2 was revised

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 2012 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers

More information

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes Java Curriculum for AP Computer Science, Student Lesson A20 1 STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes INTRODUCTION:

More information

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Object-Oriented Design. March 2005 Object Oriented Design 1

Object-Oriented Design. March 2005 Object Oriented Design 1 Object-Oriented Design March 2005 Object Oriented Design 1 Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented

More information

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner Inheritance II Lecture 34, Mon Apr 12 2010 borrowing from slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/111-10

More information

COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan

COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS Instructor: Prasun Dewan PREREQUISITE Inheritance 2 MORE INHERITANCE Inheritance Graphics Examples Inherited Variables Constructors Memory Representation

More information

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses Objectives Inheritance Students should: Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create new Java

More information

Classes and Inheritance Extending Classes, Chapter 5.2

Classes and Inheritance Extending Classes, Chapter 5.2 Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.

More information