260 likes | 374 Views
Introduction to Java. Outline. Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes Generics Exceptions Reflection. Access Levels. Private Protected Default Accessed by other classes in the same package
E N D
Outline • Classes • Access Levels • Member Initialization • Inheritance and Polymorphism • Interfaces • Inner Classes • Generics • Exceptions • Reflection
Access Levels • Private • Protected • Default • Accessed by other classes in the same package • Simply do not write an access modifier • Public
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;
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
Member Initialization (Cont.) • A constructor can invoke other constructors • Example: Public MyClass(inti) {} Public MyClass() { this(5); //Extra code }
Inheritance and Polymorphism • Similar 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 } }
Inheritance and Polymorphism (Cont.) • To call the base class version of a polymorphic method public Child extends Parent { public void f() { //Extra Code super.f(); //Extra Code } }
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.
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 ?)
Interfaces public interface SampleInterface { void f(); //No modifier, no code } public class MyClass implements SampleInterface { void f() {/*Implementation*/} }
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)
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.
Inner Classes • Example: public interface Comparator { boolisLessThan(Object o1, Object o2); }
Inner Classes • Example: class MyColl { public void getMin(Comparator c) {} }
Inner Classes • Without Inner classes: class MyComparator implements Comparator { bool compare(Object o1, Object o2) {} } MyColl m = new MyColl(); m.sort(new MyComparator());
Inner Classes • With Inner classes: MyColl m = new MyColl(); Comaprator c = new Comparator() { bool compare(Object o1, Object o2) {} } m.sort(c);
Inner Classes • Or Even: MyColl m = new MyColl(); m.sort( new Comparator() { bool compare(Object o1, Object o2) {} } );
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()); }
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()); }
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)
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.