Classes and Objects in Java

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
Classes and Objects in Java
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 10 Introduction to Objects and Classess 1.
Tutorial 6 February 4th/5th, 2015
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
JavaScript, Fourth Edition
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Inheritance in the Java programming language J. W. Rider.
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.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Objects and Classes Mostafa Abdallah
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
DR. NERMIN HAMZA Java: Lecture 2 Java life cycle 2 Java programs normally undergo four phases  Edit  Programmer writes program (and stores program.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Web Design & Development Lecture 9
Final and Abstract Classes
Classes and Objects in Java
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 8 Objects and Classes Part 1
Classes and Objects.
Classes Lecture 7 from Chapter /1/11.
Chapter 8 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes Part 01
Object-Oriented PHP (1)
OO Programming Concepts
Final and Abstract Classes
Chapter 7 Objects and Classes
Presentation transcript:

Classes and Objects in Java Java Programming Construct IV Classes and Objects in Java

Contents Introduce to classes and objects in Java. Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything we wish to represent in Java must be encapsulated in a class that defines the “state” and “behaviour” of the basic program components known as objects.

Classes create objects and objects use methods to communicate between them. They provide a convenient method for packaging a group of logically related data items and functions that work on them. A class essentially serves as a template for an object and behaves like a basic data type “int”. It is therefore important to understand how the fields and methods are defined in a class and how they are used to build a Java program that incorporates the basic OO concepts such as encapsulation, inheritance, and polymorphism.

Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()

Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. The basic syntax for a class definition: Bare bone class – no fields, no methods class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] } public class Circle { // my circle class }

Adding Fields: Class Circle with fields Add fields The fields (data) are also called the instance variables. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }

Adding Methods A class with only data fields has no life. Objects created by such a class cannot respond to any messages. Methods are declared inside the body of the class but immediately after the declaration of data fields. The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }

Adding Methods to Class Circle public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; Method Body

Data Abstraction Declare the Circle class, have created a new data type – Data Abstraction Can define variables (objects) of that type: Circle aCircle; Circle bCircle;

Class of Circle cont. aCircle, bCircle simply refers to a Circle object, not an object itself. aCircle bCircle null null Points to nothing (Null Reference) Points to nothing (Null Reference)

Creating objects of a class Objects are created dynamically using the new keyword. aCircle and bCircle refer to Circle objects aCircle = new Circle() ; bCircle = new Circle() ;

Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;

Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; Before Assignment Before Assignment P aCircle bCircle P aCircle bCircle Q Q

Automatic garbage collection Q The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future.

Accessing Object/Circle Data Similar to C syntax for accessing data defined in a structure. ObjectName.VariableName ObjectName.MethodName(parameter-list) Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0

Executing Methods in Object/Circle Using Object Methods: sent ‘message’ to aCircle Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area();

Using Circle Class // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String args[]) Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.r = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); }

Summary Classes, objects, and methods are the basic components used in Java programming. We have discussed: How to define a class How to create objects How to add data fields and methods to classes How to access data fields and methods to classes