Download presentation
Presentation is loading. Please wait.
Published byAngelica Dalton Modified over 9 years ago
1
Introduction to Java Prepared by: Ahmed Hefny
2
Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes Generics Exceptions Reflection
3
Access Levels Private Protected Default – Accessed by other classes in the same package – Simply do not write an access modifier Public
4
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;
5
Member Initialization (Cont.) Static Initialization Block static { /* You can write any code here !!!. It will be executed when the class is loaded */ } In the constructor
6
Member Initialization (Cont.) A constructor can invoke other constructors Example: Public MyClass(int i) {} Public MyClass() { this(5); //Extra code }
7
Inheritance and Polymorphism Similar to C++ Only single public inheritance public class Child extends Parent { }
8
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()
9
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(); }
10
Inheritance and Polymorphism (Cont.) To define a sealed method, use final keyword. Sealed methods cannot be overridden public MyClass { public final void sealedMethod(); }
11
Inheritance and Polymorphism (Cont.) To call the base class constructor public Child extends Parent { public Child() { super(i); //Extra Code }
12
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.
13
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 ?)
14
Interfaces public interface SampleInterface { void f();//No modifier, no code } public class MyClass implements SampleInterface { void f() {/*Implementation*/} }
15
Interfaces Can use the interface as follows SampleInterface t = new MyClass(); t.f(); You can check whether an object o implements interface I (or of class I or subclass thereof) using instanceOf if(c instanceOf I)
16
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.
17
Inner Classes Example: public interface Comparator { bool isLessThan(Object o1, Object o2); }
18
Inner Classes Example: class MyColl { public void getMin(Comparator c) {} }
19
Inner Classes Without Inner classes: class MyComparator implements Comparator { bool compare(Object o1, Object o2) {} } MyColl m = new MyColl(); m.sort(new MyComparator());
20
Inner Classes With Inner classes: MyColl m = new MyColl(); Comaprator c = new Comparator() { bool compare(Object o1, Object o2) {} } m.sort(c);
21
Inner Classes Or Even: MyColl m = new MyColl(); m.sort( new Comparator() { bool compare(Object o1, Object o2) {} } );
22
Generics Similar to STL classes Defined in java.util package Example 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 = it.next(); System.out.print(i.toString()); }
23
Generics Similar to STL classes Defined in java.util package Example 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()); }
24
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)
25
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.