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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Road Map Introduction to object oriented programming. Classes
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
JAVA MULTIPLE CHOICE QUESTION.
Inheritance and Polymorphism
null, true, and false are also reserved.
Interface.
Java Programming Language
Conditional Statements
Interfaces.
METHOD OVERRIDING in JAVA
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Introduction to Java Prepared by: Ahmed Hefny

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(int i) {} 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 attribute (why 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 }

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

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

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.