Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes

Download Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes

Post on 12-Jan-2016

214 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<p>Slide 1</p> <p>Introduction to JavaPrepared by:Ahmed Hefny</p> <p>OutlineClassesAccess LevelsMember InitializationInheritance and PolymorphismInterfacesInner ClassesGenericsExceptionsReflectionAccess LevelsPrivate</p> <p>Protected</p> <p>DefaultAccessed by other classes in the same packageSimply do not write an access modifier</p> <p>PublicMember InitializationDefault Initialization (For class members only. Locals are not initializaed)Numbers 0References nullBoolean false</p> <p>Explicit initializationE.g. Private int x = 0;</p> <p>Member Initialization (Cont.)Static Initialization Blockstatic {/* You can write any code here !!!. It will be executed when the class is loaded */}In the constructor</p> <p>Member Initialization (Cont.)A constructor can invoke other constructorsExample:Public MyClass(int i) {}</p> <p>Public MyClass() { this(5); //Extra code}</p> <p>Inheritance and PolymorphismSimilar to C++Only single public inheritance</p> <p>public class Child extends Parent{}Inheritance and Polymorphism (Cont.)In Java, all methods are virtual by default.</p> <p>Declaring a method in a child class with the same signature of a method in the base class overrides it.</p> <p>Explicitly use @override attribute (why ?)@override public void f()</p> <p>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.</p> <p>public abstract MyClass{public abstract void abstractMethod();}</p> <p>Inheritance and Polymorphism (Cont.)To define a sealed method, use final keyword. Sealed methods cannot be overridden</p> <p>public MyClass{public final void sealedMethod();}</p> <p>Inheritance and Polymorphism (Cont.)To call the base class constructor</p> <p>public Child extends Parent{public Child(){super(i);//Extra Code}}</p> <p>InterfacesAn interface represents some behavior or functionality shared among multiple classes.</p> <p>For example, Strings, dates and students can be compared but that does not justify defining a common base class for them.</p> <p>Because they can also be serialized.InterfacesAlthough a class can extend one class. It can implement any number of interfaces.</p> <p>An interface defines a set of functions without implementation and it contains no data member (why ?)Interfacespublic interface SampleInterface{void f();//No modifier, no code}</p> <p>public class MyClass implements SampleInterface{void f() {/*Implementation*/}}InterfacesCan use the interface as followsSampleInterface t = new MyClass();t.f();</p> <p>You can check whether an object o implements interface I (or of class I or subclass thereof) using instanceOfif(c instanceOf I) Inner ClassesLike 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.</p> <p> Inner ClassesExample:public interface Comparator{bool isLessThan(Object o1, Object o2);} Inner ClassesExample:class MyColl{public void getMin(Comparator c) {}} Inner ClassesWithout Inner classes:class MyComparator implements Comparator{bool compare(Object o1, Object o2) {}}</p> <p>MyColl m = new MyColl();m.sort(new MyComparator()); Inner ClassesWith Inner classes:MyColl m = new MyColl();Comaprator c = new Comparator() {bool compare(Object o1, Object o2) {}}</p> <p>m.sort(c); Inner ClassesOr Even:MyColl m = new MyColl();m.sort( new Comparator() {bool compare(Object o1, Object o2) {}});GenericsSimilar to STL classesDefined in java.util packageExample</p> <p>LinkedList l = new LinkedList();</p> <p> l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));</p> <p> Iterator it = l.iterator();</p> <p> while(it.hasNext()) { Integer i = it.next(); System.out.print(i.toString()); }</p> <p>GenericsSimilar to STL classesDefined in java.util packageExample</p> <p>LinkedList l = new LinkedList();</p> <p> l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));</p> <p> for(Integer i : l) {System.out.print(i.toString()); }</p> <p>ExceptionsSimilar to C++ with two major additionsfinally blockCode executes whether a method terminates normally or due to an exceptionsGood place for releasing resourcesException handling is obligatoryEither handle the exception (catch)Or let the caller handle it (throws)</p> <p>ReflectionAllows for invoking classes and methods known only at run time.</p> <p>Class c = class.forName(Name);The obtained object allows you to query methods and invoke them.</p>