Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.

Similar presentations


Presentation on theme: "CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1."— Presentation transcript:

1 CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1

2 CSM-Java Programming-I Lesson-1 Objectives Java Setup Instructions. Overview of Objects and Classes.

3 CSM-Java Programming-I Lesson-1 How to Program in Java 1: Edit code using any text editor 2: Compile code using javac ClassName.java Any errors? If yes, repeat 1 and 2; if no, continue 3: Run Java application java ClassName Any errors? If yes, repeat step 1, 2 and 3.

4 Java Program Lifecycle A Java program is compiled to generate byte-code. Byte-code is similar to assembly code, but hardware independent Interpreter translates from generic byte code to hardware- specific machine code Java Program User input Compiler Generates byte-code Interpreter Generates machine-code Execute machine-code Output JVM

5 Object An Object-Oriented Program consists of a group of cooperating objects, exchanging messages, for the purpose of achieving a common objective. Object: An entity that you can manipulate in your programs (by invoking methods). It has: state - attributes behaviors - operations or methods CSM-Java Programming-I Lesson-1

6 Object Example: A bank account has an account number has a current balance can be deposited into can be withdrawn from CSM-Java Programming-I Lesson-1

7 Classes Classes provide the structure for objects. A class defines the methods and types of data associated with an object. Creating an object from a class is called instantiation; an object is an instance of a particular class. For example, the Book class could describe many books, but “Big Java” is a individual book with a particular name. CSM-Java Programming-I Lesson-1

8 Declaring Java Class Example public class Book { private String bookName; //declare variable public Book(String Name) //declare constructor { bookName = Name; } public String getBookName() //declare method { return bookName; }

9 CSM-Java Programming-I Lesson-1 Object Creation Basic Syntax: new ClassName(parameters) Every object is created with a new operator. The object itself lives in memory somewhere in the JVM. Example: Book b = new Book(); –The variable b is not an object; it contains the address of the object (a reference to a Book object.)

10 CSM-Java Programming-I Lesson-1 Declaring Constructors Example public class Customer { private String cusName; //declare variable public Customer(String Name) //declare constructor { cusName = Name; }

11 Object and Class Objects are instances of classes. If Book be a class then, each individual book is a unique instance of a class Book. The variables that the object contains are called instance variables. The subroutines that the object contains are called instance methods. The attributes and operations defined by a class are for its objects, not for itself. A class is a logical construct, an object has physical reality. CSM-Java Programming-I Lesson-1

12 Object members Example public class Book { private String bookName; //declare variable public void setBookName (String Name) { bookName = Name; } public String getBookName() //declare method { return bookName; }

13 CSM-Java Programming-I Lesson-1 Accessing Object Members The “dot” operator is used to access members of the object. Examples b.setBookName(“Big Java”); b.book_id = 123;

14 CSM-Java Programming-I Lesson-1 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program, the space it occupies can be reclaimed. Java performs automatic garbage collection periodically. In other languages, the programmer has the responsibility for performing garbage collection.

15 CSM-Java Programming-I Lesson-1 this this is always a reference to the object on which the method was invoked. this can be used inside any method to refer to the current object. To pass the current object as a parameter to another method or constructor Example: public class Book { private String bookName; public setBookName(String bookName) { this.bookName = bookName; }

16 CSM-Java Programming-I Lesson-1 static A static member is a member that is only one per class, rather than one in every object created from that class. When a member is declared static, it can be accessed before any objects of its class are created. A static method can access only static variables and static methods of the class. There is no this reference because there is no specific object being operated on. classname.staticmethod();

17 CSM-Java Programming-I Lesson-1 Access Control Access control modifiers control access from other classes and inheritance by subclasses. Class members have four possible access control modifiers: private: Members declared private are accessible only in the class itself. package: Members declared with no access modifier are accessible in the class itself and are accessible to code in the same package.

18 CSM-Java Programming-I Lesson-1 Access Control protected: Members declared protected are accessible in the class itself, and are accessible to code in the same package and code in subclasses. public: Members declared with public modifier are accessible anywhere.

19 CSM-Java Programming-I Lesson-1 OOP Principle - Encapsulation Encapsulation is the mechanism that binds together the code and the data it manipulates, and keeps both safe from outside interference and misuse. Hides the implementation details of a class behind operations performed on its internal data. Forces the user to use an interface to access data.

20 OOP Principle - Inheritance Inheritance is the process by which one object acquires the properties of another object. By use of inheritance, an object need only define all of its characteristics that make it unique within its class, it can inherit its general attributes from its parent. BankAccount CheckingSavingsCDs CSM-Java Programming-I Lesson-1

21 Inheritance When a class B acquires the properties of another class A, then class B is said to have inherited class A. Here, class A is a superclass and class B is a subclass. A subclass inherits all the instance variables and methods defined by the superclass and adds its own, unique elements. The keyword extends is used to inherit a class. Eg: class B extends A {

22 CSM-Java Programming-I Lesson-1 Inheritance Every class in Java is an extended class, whether it is declared with an extends keyword or not. If a class does not explicitly extend from another class, it implicitly extends the Object class.

23 CSM-Java Programming-I Lesson-1 super The reason super exists is, so you can get access to things in the superclass that are hidden by things in the subclass. For example, super.x always refers to an instance variable named x in the superclass. Whenever a subclass calls super(), it is calling the constructor of its immediate superclass. super() must be the first statement executed inside a subclass constructor.

24 CSM-Java Programming-I Lesson-1 Method Overriding When a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the subclass. Only non-static methods can be overridden. Both signature and return type must be the same as the superclass. The throws clause of an overriding method can have fewer Exception types listed than the method in the superclass, or more specific Exception types or both.

25 CSM-Java Programming-I Lesson-1 Method Overloading In Java, it is possible for two or more methods to have the same name, as long as they have different number or types of parameters. This is called method overloading. When an overloaded method is invoked, the compiler compares the number and types of parameters to find the method that best matches the available signatures. The return type and the exceptions thrown are not counted for overloading of methods.

26 CSM-Java Programming-I Lesson-1 final A final variable is a constant. Once its value has been set, it cannot be changed. Use named constants to make your programs easier to read and maintain. Eg: final int DAYS_PER_YEAR = 365;

27 CSM-Java Programming-I Lesson-1 final A method declared final cannot be overridden. A class can also be declared final. Such a class cannot be extended. final class Security { // } A final class’s methods are implicitly final. static and private methods cannot be overridden.

28 OOP Principle – Polymorphism Polymorphism(from Greek, meaning“many forms”) is a feature that allows one interface to be used for a general class of actions, i.e. one interface, multiple methods. Polymorphism just means that different objects can respond to the same message in different ways. CSM-Java Programming-I Lesson-1

29 Abstract Classes A superclass that only defines a generalized form that will be shared by all its subclasses, leaving the implementation details to its subclasses is said to an abstract class. A concrete class has concrete methods, including implementations of any abstract methods inherited from its superclasses. Any class with abstract methods should be declared abstract.

30 CSM-Java Programming-I Lesson-1 Abstract Classes An abstract class cannot have objects because it is not complete, but it can have references. Static methods and constructors cannot be declared abstract. Any subclass of an abstract class must either implement all the abstract methods in the superclass or be itself declared abstract. A concrete method can be overridden to become abstract. It is illegal to declare a class both final and abstract.

31 CSM-Java Programming-I Lesson-1 Example: Abstract Class abstract class Shape { abstract double area(); public void display () {// concrete method // Do something } class Square extends Shape { double area() { // Do something }

32 CSM-Java Programming-I Lesson-1 Exceptions A Java exception is an object that describes an error condition occurred in the code. When an exception occurs, an object representing that exception is created and thrown in the method that caused the exception. That method may choose to handle the exception itself, or pass it on. At some point, the exception is caught and processed.

33 CSM-Java Programming-I Lesson-1 Exception Types All exception types are subclasses of class Throwable. The two subclasses of Throwable are Error and Exception. There are two types of exceptions in class Exception : 1.Checked exceptions – Checked exceptions are due to external circumstances that the programmer cannot prevent. The compiler checks that your program handles these exceptions. 2.Unchecked exceptions – Unchecked exceptions are programmers’ fault. The compiler does not require you to keep track of unchecked exceptions.

34 CSM-Java Programming-I Lesson-1 Exception Types A subclass of Exception is RuntimeException. Exceptions belonging to the subclasses of RuntimeException are unchecked expections. All other subclasses of the class Exception are checked. Exceptions of the type Error are caused by Java run-time environment OutofMemoryError is an example of type Error.

35 CSM-Java Programming-I Lesson-1 Exceptions Hierarchy Throwable ErrorException IOException ClassNotFound Exception CloneNotSupproted Exception Runtime Exception Arithmethic Exception FileNotFound Exception

36 CSM-Java Programming-I Lesson-1 Interfaces Multiple inheritance is not allowed in Java. Through interfaces Java accomplishes multiple inheritance. An "interface" in Java consists of a set of methods, without any implementations. A class can implement an interface by providing an implementation for each of the methods specified by the interface. public interface Drawable { public void draw(); }


Download ppt "CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1."

Similar presentations


Ads by Google