introduction to java prepared by: ahmed hefny. outline classes access levels member initialization...
TRANSCRIPT
![Page 1: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/1.jpg)
Introduction to Java
Prepared by:Ahmed Hefny
![Page 2: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/2.jpg)
Outline
• Classes• Access Levels• Member Initialization• Inheritance and Polymorphism• Interfaces• Inner Classes• Generics• Exceptions• Reflection
![Page 3: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/3.jpg)
Access Levels
• Private
• Protected
• Default– Accessed by other classes in the same package– Simply do not write an access modifier
• Public
![Page 4: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/4.jpg)
Member Initialization
• Default Initialization (For class members only. Locals are not initializaed)– Numbers 0– References null– Boolean false
• Explicit initialization– E.g. Private int x = 0;
![Page 5: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/5.jpg)
Member Initialization (Cont.)
• Static Initialization Blockstatic {/* You can write any code here !!!. It will be
executed when the class is loaded */}• In the constructor
![Page 6: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/6.jpg)
Member Initialization (Cont.)
• A constructor can invoke other constructors• Example:Public MyClass(int i) {}
Public MyClass() { this(5);
//Extra code}
![Page 7: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/7.jpg)
Inheritance and Polymorphism
• Similar to C++• Only single public inheritance
public class Child extends Parent{}
![Page 8: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/8.jpg)
Inheritance and Polymorphism (Cont.)
• In Java, all methods are virtual by default.
• Declaring a method in a child class with the same signature of a method in the base class overrides it.
• Explicitly use @override attribute (why ?)@override public void f()
![Page 9: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/9.jpg)
Inheritance and Polymorphism (Cont.)
• To define an abstract method, use abstract keyword. The whole class must be declared abstract if it contains an abstract method.
public abstract MyClass{public abstract void abstractMethod();}
![Page 10: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/10.jpg)
Inheritance and Polymorphism (Cont.)
• To define a sealed method, use final keyword. Sealed methods cannot be overridden
public MyClass{public final void sealedMethod();}
![Page 11: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/11.jpg)
Inheritance and Polymorphism (Cont.)
• To call the base class constructor
public Child extends Parent{public Child(){
super(i);//Extra Code
}}
![Page 12: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/12.jpg)
Interfaces
• An interface represents some behavior or functionality shared among multiple classes.
• For example, Strings, dates and students can be compared but that does not justify defining a common base class for them.
• Because they can also be serialized.
![Page 13: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/13.jpg)
Interfaces
• Although a class can extend one class. It can implement any number of interfaces.
• An interface defines a set of functions without implementation and it contains no data member (why ?)
![Page 14: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/14.jpg)
Interfaces
public interface SampleInterface{
void f(); //No modifier, no code}
public class MyClass implements SampleInterface{
void f() {/*Implementation*/}}
![Page 15: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/15.jpg)
Interfaces
Can use the interface as followsSampleInterface t = new MyClass();t.f();
You can check whether an object o implements interface I (or of class I or subclass thereof) using instanceOfif(c instanceOf I)
![Page 16: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/16.jpg)
Inner Classes
• Like C++, we can define a class nested in another one.
• In Java, we can define local inner classes in a function.
• We can define anonymous inner classes on the fly.
![Page 17: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/17.jpg)
Inner Classes
• Example:public interface Comparator{
bool isLessThan(Object o1, Object o2);}
![Page 18: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/18.jpg)
Inner Classes
• Example:class MyColl{public void getMin(Comparator c) {}}
![Page 19: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/19.jpg)
Inner Classes
• Without Inner classes:class MyComparator implements Comparator{bool compare(Object o1, Object o2) {}}
MyColl m = new MyColl();m.sort(new MyComparator());
![Page 20: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/20.jpg)
Inner Classes
• With Inner classes:MyColl m = new MyColl();Comaprator c = new Comparator() {
bool compare(Object o1, Object o2) {}}
m.sort(c);
![Page 21: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/21.jpg)
Inner Classes
• Or Even:MyColl m = new MyColl();m.sort( new Comparator() {
bool compare(Object o1, Object o2) {}});
![Page 22: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/22.jpg)
Generics• Similar to STL classes• Defined in java.util package• Example
LinkedList<Integer> l = new LinkedList<Integer>();
l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));
Iterator<Integer> it = l.iterator();
while(it.hasNext()) { Integer i = it.next(); System.out.print(i.toString()); }
![Page 23: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/23.jpg)
Generics• Similar to STL classes• Defined in java.util package• Example
LinkedList<Integer> l = new LinkedList<Integer>();
l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));
for(Integer i : l) {
System.out.print(i.toString()); }
![Page 24: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/24.jpg)
Exceptions
• Similar to C++ with two major additions– finally block• Code executes whether a method terminates normally
or due to an exceptions• Good place for releasing resources
– Exception handling is obligatory• Either handle the exception (catch)• Or let the caller handle it (throws)
![Page 25: Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes](https://reader036.vdocuments.mx/reader036/viewer/2022062314/56649e915503460f94b96915/html5/thumbnails/25.jpg)
Reflection
• Allows for invoking classes and methods known only at run time.
Class c = class.forName(“Name”);The obtained object allows you to query
methods and invoke them.