1 / 26

Introduction to Java

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

kacy
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java

  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(inti) {} 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. 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 } }

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

  14. 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 ?)

  15. Interfaces public interface SampleInterface { void f(); //No modifier, no code } public class MyClass implements SampleInterface { void f() {/*Implementation*/} }

  16. 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)

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

  18. Inner Classes • Example: public interface Comparator { boolisLessThan(Object o1, Object o2); }

  19. Inner Classes • Example: class MyColl { public void getMin(Comparator c) {} }

  20. Inner Classes • Without Inner classes: class MyComparator implements Comparator { bool compare(Object o1, Object o2) {} } MyColl m = new MyColl(); m.sort(new MyComparator());

  21. Inner Classes • With Inner classes: MyColl m = new MyColl(); Comaprator c = new Comparator() { bool compare(Object o1, Object o2) {} } m.sort(c);

  22. Inner Classes • Or Even: MyColl m = new MyColl(); m.sort( new Comparator() { bool compare(Object o1, Object o2) {} } );

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

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

  25. 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)

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

More Related