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

25
Introduction to Java Prepared by: Ahmed Hefny

Upload: angelica-dalton

Post on 12-Jan-2016

224 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.