Java Object and Class in Java. Java Naming conventions A naming convention is a rule to follow as you decide what to name your identifiers e.g. class,

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
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.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
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.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 4 Introduction to Classes, Objects, Methods and strings
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Chapter 5 Introduction to Defining Classes
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Java Programming static keyword.
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.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter No. : 2 Classes and Objects.
GC211 Data structure Lecture 3 Sara Alhajjam.
University of Central Florida COP 3330 Object Oriented Programming
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Yanal Alahmad Java Workshop Yanal Alahmad
University of Central Florida COP 3330 Object Oriented Programming
Variables ICS2O.
Encapsulation and Constructors
Object Oriented Programming in java
Chapter 5 server Side Scripting Date: 01 October 2017.
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Chap 2. Identifiers, Keywords, and Types
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Java Object and Class in Java

Java Naming conventions A naming convention is a rule to follow as you decide what to name your identifiers e.g. class, package, variable, constant, method etc. But, it is not forced to follow. So, it is known as convention not rule. Advantage of naming conventions in java By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.

class name: should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. interface name: should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. method name: should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. variable name: should start with lowercase letter e.g. firstName, orderNumber etc. package name: should be in lowercase letter e.g. java, lang, sql, util etc. constants name: should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

Object and Class In this page, we will learn about java objects and classes. In object-oriented programming technique, we design a program using objects and classes. Object is the physical as well as logical entity whereas class is the logical entity only.

Object in Java An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tengible and intengible). The example of integible object is banking system.

Object in Java An object has three characteristics: state: represents data (value) of an object. behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc. identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

Object in Java(Cont…) For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior. Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

Class in Java A class is a group of objects that has common properties. It is a template or blueprint from which objects are created. A class in java can contain: – data member – method – constructor – block – class and interface Syntax to declare a class: class { data member; method; }

Simple Example of Object and Class In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value. class Student{ int id;//data member (also instance variable) String name;//data member(also instance variable) public static void main(String args[]){ Student s1=new Student();//creating an object of Student System.out.println(s1.id+" "+s1.name); } Output:0 null

Instance variable in Java A variable that is created inside the class but outside the method, is known as instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when object(instance) is created. That is why, it is known as instance variable.

Method in Java In java, a method is like function i.e. used to expose behavior of an object. Advantage of Method – Code Reusability – Code Optimization new keyword The new keyword is used to allocate memory at runtime.

Example of Object and class that maintains the records of students In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method on it. Here, we are displaying the state (data) of the objects by invoking the displayInformation method.

class Student{ int rollno; String name; void insertRecord(int r, String n){ //method rollno=r; name=n; } void displayInformation(){System.out.println(roll no+" "+name);}//method

public static void main(String args[]){ Student s1=new Student(); Student s2=new Student(); s1.insertRecord(111,"Karan"); s2.insertRecord(222,"Aryan"); s1.displayInformation(); s2.displayInformation(); }

As you see in the above figure, object gets the memory in Heap area and reference variable refers to the object allocated in the Heap memory area. Here, s1 and s2 both are reference variables that refer to the objects allocated in memory.

Another Example of Object and Class There is given another example that maintains the records of Rectangle class. Its exaplanation is same as in the above Student class example. class Rectangle{ int length; int width; void insert(int l,int w){ length=l; width=w; }

void calculateArea(){System.out.println(length*width);} public static void main(String args[]){ Rectangle r1=new Rectangle(); Rectangle r2=new Rectangle(); r1.insert(11,5); r2.insert(3,15); r1.calculateArea(); r2.calculateArea(); } Output:55 45

What are the different ways to create an object in Java? There are many ways to create an object in java. They are: – By new keyword – By newInstance() method – By clone() method – By factory method etc. We will learn, these ways to create the object later.

Annonymous object Annonymous simply means nameless. An object that have no reference is known as annonymous object. If you have to use an object only once, annonymous object is a good approach.

class Calculation{ void fact(int n){ int fact=1; for(int i=1;i<=n;i++){ fact=fact*i; } System.out.println("factorial is "+fact); } public static void main(String args[]){ new Calculation().fact(5);//calling method with annonym ous object }

Creating multiple objects by one type only We can create multiple objects by one type only as we do in case of primitives. Rectangle r1=new Rectangle(),r2=new Rectang le();//creating two objects

class Rectangle{ int length; int width; void insert(int l,int w){ length=l; width=w; } void calculateArea(){ System.out.println(length*width);}

public static void main(String args[]){ Rectangle r1=new Rectangle(),r2=new Rectangle(); //creating two objects r1.insert(11,5); r2.insert(3,15); r1.calculateArea(); r2.calculateArea(); } Output:55 45