Object Oriented Programming I (1301108 ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.

Slides:



Advertisements
Similar presentations
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
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,
Saravanan.G.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
UML Basics & Access Modifier
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Object Oriented Programming I
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Programming in Java CSCI-2220 Object Oriented Programming.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Constructors & Garbage Collection Ch. 9 – Head First Java.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Objects as a programming concept
3 Introduction to Classes and Objects.
Object Oriented Programming
ATS Application Programming: Java Programming
CSC 113 Tutorial QUIZ I.
Overloading and Overriding
Classes & Objects: Examples
Encapsulation and Constructors
METHOD OVERRIDING in JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
JAVA Constructors.
Classes and Objects CGS3416 Spring 2019.
ITE “A” GROUP 2 ENCAPSULATION.
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011

OUTLINE Classes, Objects. Creating Objects Constructors Access Modifiers –Private –Public –Default –Protected

What Is a Java Class? Classes are the fundamental building blocks of a Java program. You can define an Employee class as follows: class Employee { int age; …….. ……… double salary; } public class MainClass { private int aField; public void aMethod() { } }

Creating an Object As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word is used to create new objects.

Creating an Object There are three steps when creating an object from a class: –Declaration. A variable declaration with a variable name with an object type. –Instantiation. The 'new' key word is used to create the object. –Initialization. The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Creating an Object (Example) public class Object_Exp { public static void main(String[] args) { Puppy myPuppy = new Puppy( "tommy" ); } class Puppy { public Puppy(String name){ // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } OUTPUT Passed Name is :tommy

Constructors When discussing about classes one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class. Each time a new object is created at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

Constructors class Puppy{ public Puppy() { } public Puppy(String name){ // This constructor has one parameter, name. }}

Accessing Instance Variables and Methods public class Test { public static void main(String[] args) { Puppy myPuppy = new Puppy( "tommy" ); // Object creation myPuppy.setAge( 2 ); // Call class method to set puppy's age myPuppy.getAge( ); // Call another class method to get puppy's age //You can access instance variable as follows as well System.out.println("Variable Value :" + myPuppy.puppyAge ); } } class Puppy { int puppyAge; public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("Puppy's age is :" + puppyAge ); return puppyAge;} } OUTPUT Passed Name is :tommy Puppy's age is :2 Variable Value :2

Access Modifiers Private Public Default Protected

Access Modifiers Attribute :Permitted Access No access attributeAccessible from methods in any class in the same package PublicAnywhere as long as the class has been declared as public PrivateAccessible from methods inside the class ProtectedAccessible from methods in the same package and from any subclass

Access Level From classes in the other packages From classes in the same package From child classes From the same class publicyes protectednoyes defaultnoyesnoyes privateno yes Access Modifiers