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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

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.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA BASICS Prepared by The Smartpath Information Systems
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Final and Abstract Classes
Inheritance and Polymorphism
Java Programming Language
Java – Inheritance.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

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

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.

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

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

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

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

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

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

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

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

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

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;

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.

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

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

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.

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.

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.

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

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 {

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.

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.

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.

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.

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;

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.

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

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.

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.

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 }

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.

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.

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.

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

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