Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0."— Presentation transcript:

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

2

3 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

4 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

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

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

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

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

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

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

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

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

13 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

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

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

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

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

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

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


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

Similar presentations


Ads by Google