Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.

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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction To Object-Oriented Programming. Object-Oriented Programming Class: Code that defines the behavior of a Java programming element called an.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
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.
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.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Learners Support Publications Classes and Objects.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
SEEM Java – Basic Introduction, Classes and Objects.
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.
Classes, Interfaces and Packages
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP Basics Classes & Methods (c) IDMS/SQL News
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Constructors and Destructors
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Classes (Part 1) Lecture 3
Interface.
Java Primer 1: Types, Classes and Operators
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Introduction to Object-oriented Program Design
Interface.
Classes & Objects: Examples
Sampath Kumar S Assistant Professor, SECE
Constructors and Destructors
Classes Lecture 7 from Chapter /1/11.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Dr. R Z Khan Handout-3 Classes
Introduction to Classes and Objects
Presentation transcript:

Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0

Create classes and declare their members in Java programme. 1 Define method and identify its uses. 2 Add methods in Java programmes. 3 Identify the built-in methods in Java library. 4 Write Java programme using methods. 5

Create objects for a class in Java programmes. 6 Develop Java programme using classes and objects. 7 Define constructors and identify its uses. 8 Add constructors in Java programmes. 9 Write Java programme using constructors. 10

Introduction Java is an object-oriented programming language. The concept behind object-oriented programming is implemented in Java using classes and objects.  Classes – A class is a user-defined data type. Class is a container which holds variables and methods.  Objects – The Objects are created to access the variables and methods from classes.

Example - Creating a Class class Book { //body of the class Book }

Variable and Method Declaration Variables and methods are declared in the body of class. Methods are necessary for operating with the data contained in the class. Method declarations have four basic parts:  The name of the method (methodname).  The type of the value the method returns (type).  A list of parameters (parameter-list).  The body of the method.

Variable and Method Declaration class Act5F { String name, int age; void insert(String n, int a) { name = n; age = a; } void show() { System.out.println(“My Name is “ + name); System.out.println(“My Age is “ + age); } public static void main(String[]args) { Act5F obj = new Act5F(); obj.insert(“Nabila”, 15); obj.show(); }

Creating Objects Object is an entity that consists of variables and methods to manipulate those variables. The class members can be accessed through objects. For example, create an object for class Book. Book b1; b1 = new Book( Book b1 = new Book( );

Creating Objects class Act5F { String name, int age; void insert(String n, int a) { name = n; age = a; } void show() { System.out.println(“My Name is “ + name); System.out.println(“My Age is “ + age); } public static void main(String[]args) { Act5F obj = new Act5F(); obj.insert(“Nabila”, 15); obj.show(); }

Assigning Object Reference You can create an object for a class and assign it to another object. Book b1 = new Book( ); Book b2 = b1;

Assign Value to Instance Variables You can assign values to the Instance variables through the methods and access the class members. Example Book b1 = new Book( ); b1.getData("Java 2 A Beginner's Guide","Herbert Schildt",100);

class Act5F { String name, int age; void insert(String n, int a) { name = n; age = a; } void show() { System.out.println(“My Name is “ + name); System.out.println(“My Age is “ + age); } public static void main(String[]args) { Act5F obj = new Act5F(); obj.insert(“Nabila”, 15); obj.show(); } Assign Value to Instance Variables

Syntax to Assign Value to Instance Variables. (parameter-list); Here, 1.objectname is the name of the object. 2.methodname is the method of the class that you wish to call. 3.Parameter-list is the comma separated list of values that must match in type and number with the parameter list of the methodname declared in the class.

Access Data members in a class The data members in a class is accessed using the dot operator. Example : b1.book_name = "Java 2 A Beginner's Guide"; b1.author_name = "Herbert Schildt“; b1.no_of_pages = 100;

Constructors Is a special method that enables an object to initialise itself when it is created. It is used to initialise the data members of a class. Constructor gets automatically called when an object is created.

Constructors Characteristics of Constructors Constructors have the same name as the class name. The constructor allocates sufficient memory space for the object. They do not return any value. The syntax of a constructor is similar to that of a method. Once defined the constructor is automatically called immediately when the object is created before the new operator completes its job. Java creates a default constructor when you do not define a constructor.

Example - Constructor class Book { Book(String bname, String aname, int nopages) { book_name = bname; author_name = aname; no_of_pages = nopages; } public static void main(String args[]) { Book b1 = new Book ("Java 2","Herbert Schildt",100); }

Example – without Constructor class Book { void getData(String bname, String aname, int nopages) { book_name = bname; author_name = aname; no_of_pages = nopages; } public static void main(String args[]) { Book b1 = new Book (); b1.getData ("Java 2","Herbert Schildt",100); }