1 Java Methods & Classes – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Lab#1 (14/3/1431h) Introduction To java programming cs425
ITEC200 – Week03 Inheritance and Class Hierarchies.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Introduction to Java and Applet. 2 Download Java Compiler (1)
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
 Java is:  platform independent programming language  similar to C++ in syntax  similar to Smalltalk in mental paradigm  Java has some interesting.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
Programming Languages and Paradigms Object-Oriented Programming.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Know Your Java. Java is special Java source code Byte code/ native code Object code on windows Object code on Dos Object code on Lynux.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Concepts
JAVA MULTIPLE CHOICE QUESTION.
Inheritance and Polymorphism
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
Java Programming Language
Classes, Encapsulation, Methods and Constructors (Continued)
Object Oriented Programming
Java – Inheritance.
Problem Solving, Object-Oriented Design and Java
Problem Solving, Object-Oriented Design and Java
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
Corresponds with Chapter 5
Presentation transcript:

1 Java Methods & Classes – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

Classes are Object Definitions OOP - object oriented programming code built from objects In Java these are called classes Each class definition is coded in a separate.java file Name of the object must match the class/object name

The three principles of OOP Encapsulation –Objects hide their functions (methods) and data (instance variables) Inheritance –Each subclass inherits all variables of its superclass Polymorphism –Interface same despite different data types car auto- matic manual Super class Subclasses draw()

Simple Class and Method Class Fruit{ int grams; int cals_per_gram; int total_calories() { return(grams*cals_per_gram); }

Methods A method is a named sequence of code that can be invoked by other Java code. A method takes some parameters, performs some computations and then optionally returns a value (or object). Methods can be used as part of an expression statement. public float convertCelsius(float tempC) { return( ((tempC * 9.0f) / 5.0f) ); }

Method Signatures A method signature specifies: –The name of the method. –The type and name of each parameter. –The type of the value (or object) returned by the method. –The checked exceptions thrown by the method. –Various method modifiers. –modifiers type name ( parameter list ) [throws exceptions ] public float convertCelsius (float tCelsius ) {} public boolean setUserInfo ( int i, int j, String name ) throws IndexOutOfBoundsException {}

Public/private Methods/data may be declared public or private meaning they may or may not be accessed by code in other classes … Good practice: –keep data private –keep most methods private well-defined interface between classes - helps to eliminate errors

Using objects Here, code in one class creates an instance of another class and does something with it … Fruit plum=new Fruit(); int cals; cals = plum.total_calories(); Dot operator allows you to access (public) data/methods inside Fruit class

Constructors The line plum = new Fruit(); invokes a constructor method with which you can set the initial data of an object You may choose several different type of constructor with different argument lists eg Fruit(), Fruit(a)...

Overloading Can have several versions of a method in class with different types/numbers of arguments Fruit() {grams=50;} Fruit(a,b) { grams=a; cals_per_gram=b;} By looking at arguments Java decides which version to use

Java Development Kit javac - The Java Compiler java - The Java Interpreter jdb - The Java Debugger appletviewer -Tool to run the applets javap - to print the Java bytecodes javaprof - Java profiler javadoc - documentation generator javah - creates C header files