introduction to java prepared by: ahmed hefny. outline classes access levels member initialization...

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




2 download

Embed Size (px)


Slide 1

Introduction to JavaPrepared by:Ahmed Hefny

OutlineClassesAccess LevelsMember InitializationInheritance and PolymorphismInterfacesInner ClassesGenericsExceptionsReflectionAccess LevelsPrivate


DefaultAccessed by other classes in the same packageSimply do not write an access modifier

PublicMember InitializationDefault Initialization (For class members only. Locals are not initializaed)Numbers 0References nullBoolean false

Explicit initializationE.g. Private int x = 0;

Member Initialization (Cont.)Static Initialization Blockstatic {/* You can write any code here !!!. It will be executed when the class is loaded */}In the constructor

Member Initialization (Cont.)A constructor can invoke other constructorsExample:Public MyClass(int i) {}

Public MyClass() { this(5); //Extra code}

Inheritance and PolymorphismSimilar to C++Only single public inheritance

public class Child extends Parent{}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()

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();}

Inheritance and Polymorphism (Cont.)To define a sealed method, use final keyword. Sealed methods cannot be overridden

public MyClass{public final void sealedMethod();}

Inheritance and Polymorphism (Cont.)To call the base class constructor

public Child extends Parent{public Child(){super(i);//Extra Code}}

InterfacesAn 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.InterfacesAlthough 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 ?)Interfacespublic interface SampleInterface{void f();//No modifier, no code}

public class MyClass implements SampleInterface{void f() {/*Implementation*/}}InterfacesCan 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) 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.

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) {}}

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) {}}

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

LinkedList l = new LinkedList();

l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));

Iterator it = l.iterator();

while(it.hasNext()) { Integer i =; System.out.print(i.toString()); }

GenericsSimilar to STL classesDefined in java.util packageExample

LinkedList l = new LinkedList();

l.add(new Integer(3)); l.add(new Integer(4)); l.add(new Integer(5));

for(Integer i : l) {System.out.print(i.toString()); }

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)

ReflectionAllows 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.


View more >