תוכנה 1 תרגול 13 – סיכום.

Slides:



Advertisements
Similar presentations
תוכנה 1 סמסטר א ' תשע " ב תרגול מס ' 7 * מנשקים, דיאגרמות וביטים * לא בהכרח בסדר הזה.
Advertisements

בתרגול הקודם הורשה: –ניתן להרחיב רק מחלקה אחת –כל מה שלא private – עובר בהורשה –המילה השמורה super –יצירת היררכיה –Object היא שורש ההיררכיה –דריסה אופרטור.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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.
1 תוכנה 1 תרגול 14 – סיכום. 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב'
Exam Example Questions
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Templates תבניות.
1 תוכנה 1 ג ' אווה תרגול י " ג – סיכום הסיכומים ליאור שפירא ומתי שמרת.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Java Implementation: Part 3 Software Construction Lecture 8.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
מבוא למדעי המחשב לתעשייה וניהול הרצאה 7. סברוטינות subroutines.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Polymorphism CS 162 (Summer 2009).
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
SCJP 5, 1/7 Declarations, Initialization and Scoping
OOP Tirgul 12.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Exceptions, Interfaces & Generics
Methods Attributes Method Modifiers ‘static’
University of Central Florida COP 3330 Object Oriented Programming
תוכנה 1 תרגול 13 – סיכום.
תוכנה 1 תרגול 13 – סיכום.
C# In many ways, C# looks similar to Java
Chapter 9 Inheritance and Polymorphism
Interface.
Enumerations & Annotations
CMSC 202 Interfaces.
Extending Classes.
Enumerations & Annotations
Java Programming Language
מבוא לתכנות בWINDOWS ב#C
The Building Blocks Classes: Java class library, over 1,800 classes:
Interfaces.
תוכנה 1 תרגול 13 – סיכום.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
METHOD OVERRIDING in JAVA
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Enumerations & Annotations
CMSC 202 Interfaces.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CMSC 202 Interfaces.
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

תוכנה 1 תרגול 13 – סיכום

בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות (למעט הרצאת אורח בשבוע 12) כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות

קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i);  int foo2(int i); } The modifiers of foo1 and foo2 are the same.

מנשקים public interface Foo { public void bar() throws Exception; } public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); public static void main(String args[]) { Foo foo = new FooImpl(); foo.bar(); Code: ex#1 האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? שגיאת קומפילציה: "Unhandled exception type Exception"

מנשקים - המשך public interface Foo { public void bar() throws Exception; } public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); public static void main(String args[]) { FooImpl foo = new FooImpl(); foo.bar(); האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? פלט: "No exception is thrown"

מנשקים וירושה Consider the following class hierarchy: Interface Animal {…} class Dog implements Animal{…} class Poodle extends Dog {…} class Labrador extends Dog {…} Which of the following lines (if any) will not compile? Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; Animal Dog Poodle Labrador poodle = (Poodle) dog; No compilation error Runtime Exception Compilation Error Type mismatch: cannot convert from Dog to Poodle Labrador labrador = (Labrador) dog; No compilation error No Runtime Exception

מנשקים וירושה class A { public void print() { System.out.println("A"); } class B extends A implements C { interface C { void print(); האם יש שגיאה? אין שגיאות קומפילציה public כברירת מחדל

מנשקים וירושה class A { void print() { System.out.println("A"); } class B extends A implements C { interface C { void print(); האם יש שגיאה? שגיאת קומפילציה: The inherited package method A.print() cannot hide the public abstract method in C

The following table shows the access to members permitted by each modifier

דריסה של שירותים public class A { public void print() { System.out.println("A"); } public class B extends A { System.out.println("B"); public class C { public static void main(String[] args){ B b = new B(); A a = b; b.print(); a.print(); } אין צורך ב-casting Code: ex#4 הפלט: B האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט?

דריסה של שירותים וניראות public class A { public void print() { System.out.println("A"); } public class B extends A { protected void print() { System.out.println("B"); public class C { public static void main(String[] args) { B b = new B(); b.print(); } Code: ex#4 שגיאת קומפילציה: "Cannot reduce the visibility of the inherited method from A" האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט?

דריסה של שירותים וניראות (2) public class A { protected void print() { System.out.println("A"); } public class B extends A { public void print() { System.out.println("B"); public class C { public static void main(String[] args) { B b = new B(); b.print(); } Code: ex#4 האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? הפלט: B

הורשה הפלט: האם הקוד מתקמפל? אם לא, למה? A.bar() public class A { public void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); public class B extends A { public void foo() { System.out.println("B.foo()"); } public static void main(String[] args) { A a = new B(); a.bar(); Code: ex#5 הפלט: A.bar() B.foo() האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט?

הורשה (2) האם הקוד מתקמפל? אם לא, למה? הפלט: public class A { private void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); public class B extends A { public void foo() { System.out.println("B.foo()"); } public static void main(String[] args) { A a = new B(); a.bar(); Code: ex#5 האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? הפלט: A.bar() A.foo()

הורשה ובנאים הפלט: What is the output? B.foo(): bar = null public class A { String bar = "A.bar";   A() { foo(); } public void foo() { System.out.println("A.foo(): bar = " + bar); } public class B extends A { String bar = "B.bar"; B() { foo(); } System.out.println("B.foo(): bar = " + bar); public class C { public static void main(String[] args) { A a = new B(); System.out.println("a.bar = " + a.bar); a.foo(); } הפלט: B.foo(): bar = null B.foo(): bar = B.bar a.bar = A.bar What is the output? Code: ex#6

הורשה ובנאים (2) הפלט: in B: no args. in A: no args. in C: no args. public class A { protected B b = new B(); public A() { System.out.println("in A: no args."); } public A(String s) { System.out.println("in A: s = " + s); } } public class B { public B() { System.out.println("in B: no args."); }   public class C extends A { protected B b; public C() { System.out.println("in C: no args."); } public C(String s) { System.out.println("in C: s = " + s); } public class D { public static void main(String args[]) { C c = new C(); A a = new C(); הפלט: in B: no args. in A: no args. in C: no args. What is the output?

הורשה ובנאים (3) הפלט: in B: no args. in A: no args. in C: s = c public class A { protected B b = new B(); public A() { System.out.println("in A: no args."); } public A(String s) { System.out.println("in A: s = " + s); } } public class B { public B() { System.out.println("in B: no args."); }   public class C extends A { protected B b; public C() { System.out.println("in C: no args."); } public C(String s) { System.out.println("in C: s = " + s); } public class D { public static void main(String args[]) { C c = new C("c"); A a = new C(“a"); הפלט: in B: no args. in A: no args. in C: s = c in C: s = a What is the output?

דריסה והעמסה של שירותים public class A {       public float foo(float a, float b) throws IOException { } public class B extends A { … אילו מהשירותים הבאים ניתן להגדיר ב- B? 1. float foo(float a, float b){…} 2. public int foo(int a, int b) throws Exception{…} 3. public float foo(float a, float b) throws Exception{…} 4. public float foo(float p, float q) {…}

הורשה ודריסת שירותים public class A { public void foo() {…} } public class B extends A { האם אפשר לקרוא ל-foo של A מתוך B? תשובה: דרך super.foo()

הורשה ודריסת שירותים (2) public class A { public void foo() {…} } public class B extends A { public class C extends B { האם אפשר לקרוא ל-foo של A מתוך C? תשובה: אי אפשר, super.super.foo() - לא חוקי

מחלקות פנימיות אילו משתנים מ- a-e נגישים מהשורה המסומנת? public class Test { public int a = 0; private int b = 1; public void foo(final int c) { int d = 2; class InnerTest { private void bar(int e) { } אילו משתנים מ- a-e נגישים מהשורה המסומנת? תשובה: כולם חוץ מ-d

Only the point where it is defined מחלקות פנימיות - סיכום Fields access Interface Inner Scope Type Only static yes no member Static nested Static and non-static Inner non-static final local variables or parameters that are accessible in the scope of the block Local scope local Only the point where it is defined anonymous

enum All enums implicitly extend java.lang.EnumAn enum cannot extend anything else. The constructor for an enum type must be package-private or private access. You cannot invoke an enum constructor yourself.  fixed set of constants

enum Output: Mondays are bad. SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY static values method that returns an array containing all of the values of the enum in the order they are declared

אוספים גנריים ניתן (ואפילו רצוי) לכתוב גם: new HashSet<>();

אוספים גנריים האם אנחנו חייבים להצהיר על טיפוס סטטי שהוא HashSet? בד"כ נשתמש בטיפוס הכללי יותר Set אלא אם כן אנחנו נדרשים ספציפית ל HashSet. למשל במקרים הבאים: אנחנו רוצים להפעיל מתודה שיש ל HashSet אך לא ל Set (יש כזו בכלל?) אנחנו משתמשים בשירות שדורש לקבל רק HashSet ולא Set.

אוספים גנריים מדוע הפונקציה דורשת לקבל HashSet? בד"כ נשתמש בטיפוס כמה שיותר כללי. האם נוכל לשלוח לפה כל Set? ע"מ המימוש שלה, אין סיבה שלא. למען האמת, נוכל לשלוח אפילו Collection.

אוספים גנריים האם יש עוד משהו שנוכל לשפר בקוד? נשים לב כי המימוש של func לא מחייב אותנו לקבל אוסף של מחרוזות. הדרישה היחידה היא שאברי האוסף יממשו את toString, מה שמובטח לכל אובייקט ב Java.

אוספים גנריים נשתמש ב <?> ע"מ לאפשר שימוש באוספים של כל טיפוס אפשרי. שימו לב, להגדיר את set מטיפוס Collection<Object> לא ישיג את אותה המטרה, כיוון שאז נוכל להפעיל את הפונקציה הזו רק עם אובייקט מטיפוס סטטי Collection<Object>

אוספים גנריים נשתמש ב <?> ע"מ לאפשר שימוש באוספים של כל טיפוס אפשרי. שימו לב, להגדיר את set מטיפוס Collection<Object> לא ישיג את אותה המטרה, כיוון שאז נוכל להפעיל את הפונקציה הזו רק עם אובייקט מטיפוס סטטי Collection<Object>

אוספים גנריים