1 תוכנה 1 ג ' אווה תרגול י " ג – סיכום הסיכומים ליאור שפירא ומתי שמרת.

Slides:



Advertisements
Similar presentations
the java language BY SA introduced in 1995 by Sun Microsystems.
Advertisements

CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter7; public class Circle { private double radius; public Circle(double radius) { radius=radius; } public.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
1 תוכנה 1 תרגול 14 – סיכום. 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public.
Final Review.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
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.
“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.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Consider the following Which of the following will cause a java.lang.ClassCastException? a)Alpha a = x; b)Foo f= (Delta)x; c)Foo f= (Alpha)x; d)Beta b.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Interfaces and Polymorphism CS 162 (Summer 2009).
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
תוכנה 1 תרגול 13 – סיכום.
תוכנה 1 תרגול 13 – סיכום.
CS240: Advanced Programming Concepts
Review for the Final Exam
CSC 113 Tutorial QUIZ I.
null, true, and false are also reserved.
CMSC 202 Interfaces.
Extending Classes.
Introduction to Java Programming
תוכנה 1 תרגול 13 – סיכום.
תוכנה 1 תרגול 13 – סיכום.
METHOD OVERRIDING in JAVA
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
مظفر بگ محمدی دانشگاه ایلام
Polymorphism and Type Casting
class PrintOnetoTen { public static void main(String args[]) {
CMSC 202 Interfaces.
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 Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 תוכנה 1 ג ' אווה תרגול י " ג – סיכום הסיכומים ליאור שפירא ומתי שמרת

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

3 מנשקים 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(); } Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Compilation Error: "Unhandled exception type Exception"

4 מנשקים 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(); } Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Output: No exception is thrown

5 מנשקים וירושה 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; poodle = (Poodle) dog; -No compilation error -Runtime Exception Animal Dog LabradorPoodle - Compilation Error Type mismatch: cannot convert from Dog to Poodle Labrador labrador = (Labrador) animal; -No compilation error -No Runtime Exception

6 מנשקים וירושה class A { public void print() { System.out.println("A"); } class B extends A implements C { } interface C { void print(); } No compilation errors Is there an error? public by default

7 מנשקים וירושה class A { void print() { System.out.println("A"); } class B extends A implements C { } interface C { void print(); } Compilation error: The inherited package method A.print() cannot hide the public abstract method in C Is there an error?

8 Method Overloading & Overriding public class A { public float foo(float a, float b) throws IOException{ } public class B extends A { … } Which of the following methods can be defined in 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) {…} Answer: 2 and 4

9 Method Overriding public class A { public 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(); A a = b; b.print(); a.print(); } Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? The output is: B Casting is unneeded

10 Method Overriding & Visibility 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(); } Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Compilation error: "Cannot reduce the visibility of the inherited method"

11 What is the output? Method Overriding & Visibility 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(); } The output is: B

12 Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Inheritance public class A { public void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); } The output is: A.bar() B.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(); }

13 The output is: A.bar() A.foo() Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Inheritance 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(); }

14 Does the code compile? If no, why? Does the code throw a runtime exception? If yes, why? If no, what is the output? Inheritance package a; public class A { public void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); } The output is: A.bar() B.foo() package b; 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(); }

15 Inheritance public class A { public void foo() {…} } public class B extends A { public void foo() {…} } How can you invoke the foo method of A within B ? Answer: Use super.foo()

16 Inheritance public class A { public void foo() {…} } public class B extends A { public void foo() {…} } public class C extends B { public void foo() {…} } How can you invoke the foo method of A within C ? Answer: Not possible (super.super.foo() is illegal)

17 Inheritance & Constructors 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(); } public void foo() { System.out.println("B.foo(): bar = " + bar); } public class D { public static void main(String[] args) { A a = new B(); System.out.println(“a.bar = “ + a.bar); a.foo(); } What is the output?The output is: B.foo(): bar = null B.foo(): bar = B.bar a.bar = A.bar B.foo(): bar = B.bar

18 Inheritance & Constructors 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(); } What is the output?The output is: in B: no args. in A: no args. in C: no args. in B: no args. in A: no args. in C: no args.

19 Inheritance & Constructors 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"); } What is the output?The output is: in B: no args. in A: no args. in C: s = c in B: no args. in A: no args. in C: s = a

20 Compilation error without this line Inheritance & Constructors 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"); } What will happen if we remove this line?

21 Inheritance & Constructors public class A { String bar = "A.bar"; } public class B extends A { String bar = "B.bar"; B() { foo(); } public void foo() { System.out.println("B.foo(): bar = " + bar); } public static void main(String[] args) { A a = new B(); System.out.println(a.bar); a.foo(); } Compilation Error: "The method foo is undefined for the type A" Will this compile? Will there be a RTE? What is the result?

22 Only a, b, c and e are accessible at the highlighted line. Inner Class 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) { } Which variables ( a, b, c, d, e ) are accessible at the highlighted line?

23 בחינה באופק ! הבחינה ב-15 בפברואר ב-2 בצהריים עצות לקראת המבחן: תתכוננו תשתו הרבה מים להשתדל להימנע מתשובות כאלו בהצלחה !