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.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Writing Classes (Chapter 4)
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.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming in Java CSCI-2220 Object Oriented Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
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.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
OOP Basics Classes & Methods (c) IDMS/SQL News
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
Objects as a programming concept
Objects as a programming concept
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Java Programming Language
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Object Oriented Programming in java
Object Oriented Programming in java
Final and Abstract Classes
Presentation transcript:

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 instances of a class. Creating a class is referred to as ‘instantiating’. A Java class uses variables to define data fields and methods to define actions. A class method/s known as constructors are designed to perform initialization of data fields of objects, and can also perform any other actions.

This program begins with // in line 1, indicating comments in a line. Line 2 indicates the program file is in a package called welcome In line 3, public class Welcome is a programmer defined class Class is a keyword(reserverd by Java) that is used as a prefix to a class name In line 5, public static void main (String []args) indicates the main method which is void thus it returns nothing. In line 7, System.out.println (“Welcome to Java 2015”) instructs the computer to perform an action thus it will print a string of characters that are within the double quotes.

State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an instance variables make up the object's state. You must always refer to it with the object that you declared, as shown in the next example: ClassName myObj = new ClassName(); myObj.theVariable = 90;

Behaviour (methods) Methods (called functions or procedures in other languages) actually assist you in modularizing a program into self-contained manageable units. The main method is called once to begin program execution. When programmers create a class, they create methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Constants and variables Variables should start with a lowercase letter, short, meaningful names, which sound good to us as programmers. Some examples:  buttonWidth  accountBalance  myString A constant variable is a variable that contains a value which does not change for the entire program, thus it should be declared with the keyword final. Constants should be named using uppercase letters with underscore characters as separators. private final int INCREMENT //Declares a constant called INCREMENT of type int that cannot be modified

//Declaring class called Dog public class Dog { //Declaring variables name and colour of type string String name; String colour; public void eat() { //method called eat //Eat code in here } public void bark() { //Bark code in here } }

Access modifiers A class can consist of modifiers as well. Access modifiers determine the visibility or accessibility of an object’s attributes and methods to other objects. Before we can begin implementing our design, we must consider which attributes and methods of our classes should be public and which should be private. A modifier is a keyword that is added in front of the class and is used to describe how the class can be accessed or used. Variables or methods declared with access modifier private are accessible only to the methods of the class to which they are declared.

Public (class) modifier A public class may be accessed from anywhere. Classes that are not public can only be accessed from other classes that are within the same directory. public class Dog { //Class body } When a class is declared public, it is visible to all other classes. Take note that you may only have one public class in each file. The name of the file must also be the same as the name of the public class. When using the access modifier public, it means that that classes, methods and data fields can be accessed from other classes.

Final (class) modifier A final class may not be extended. The compiler will return an error if you try to extend a class with a final modifier. final class Dog { //Class body }

Abstract (class) modifier An abstract class is used to declare one or more abstract methods. A class cannot be both final and abstract. It can be public and abstract, though. abstract class Dog { //Class body }

Default (class) modifier When you do not specify an access modifier for a class, the class will have default/package access. This means that the class will only be visible, and can only be used, in the current package or directory.

Public (member) modifier Public access for members of a class works in the same way as it does with classes. The members are visible to, and can be used by, any other class.

Default (member) modifier Members with default access are only visible to other classes in the same package. This happens when there is no access modifier declared, then by default the classes, method and data fields are accessible by any class in the same package. This is known as package-access.

Protected (member) modifier Protected members are the same as default members, but are also visible to subclasses of a class that can be in any other package.

Private (member) modifier Private members are only visible to the class and cannot be used by any other class. A private data field cannot be accessed by an object from outside the class that defines the private field. To make a private data field accessible, provide a get method to return its value and provide a set method to set a new private data field value.

The this reference Every object can access a reference to itself by using the keyword this also known as the this reference. The this keyword is the name of a reference that refers to a calling object itself. Its common uses are to reference a class's hidden data fields. A hidden instance variable can be accessed by using the keyword this.

Set and Get methods Set methods are also known as mutator methods as they change the object’s state. This means that they modify values of instance variables. Get methods are also known as accessor/query methods.

Composition A class can have reference to objects of other classes as members. This is called ‘composition’ and is usually termed the has-a relationship.

Garbage collection through the method finalize The method finalize is a method of class object under package java.lang. The JVM performs automatic garbage collection to reclaim memory occupied by redundant objects. This means that when there are no more references to an object it is eligible to be collected. The finalize method is called by the garbage collector to terminate an object just before it reclaims memory. This method finalize has no argument list and always has return type void.

Static class members The declaration begins with keyword static. This is normally used when every object has its own copy of all the instance variables of the class, therefore one copy of a variable will be shared by all objects of a class thus we make use of static variables to represent class wide information. A static cannot access non-static class members, because a static method can be called even when no objects of the class have been instantiated, thus the this reference cannot be used in a static method.

Creating packages Packages are used to ease complexity and be able as programmers to manage application components. They also aid in software reuse as well as unique naming convention. This basically means that each class in the Java API belongs to a package that contains a cluster of related classes.

Constructors Each class you declare can provide a special method called constructor that can be used to initialise an object of a class when the object is created. A constructor must have the same name as the class itself (defining class). Constructors do not have a return type - not even void. Constructors are invoked using the new operator when an object is created, which plays the role of object initialisation Java requires a constructor call for every object that is created. A constructor must have the same name as the class and it uses the keyword new which requests memory to store the object, and then calls the corresponding class’s constructor to initialise the object. A constructor without arguments is referred to as a no-argument constructor. A class that is defined without a constructor calls a no-argument constructor by default.

Overloaded constructors This enables objects of a class to be initialised in different ways. To achieve this one should simply provide multiple constructors with different signatures or parameter/argument list.

Method overloading Methods of the same name can be declared in the same class having different set of arguments or parameters. When an overloaded method is called, the Java compiler selects the appropriate method based on the arguments and their signature.

Method overriding When you define a method in your subclass that has exactly the same signature as a method in the superclass, the method is overridden and replaces the inherited method. When overriding methods, you are not allowed to make the methods more private, for example, you cannot define a public method as protected.

Relational operators High-level programming like Java provides selection statements that allow choice of action with alternative courses. Java selection statements use conditions, which are Boolean in expressions. This means that conditions are expressions that can be true or false, thus a computer program should be able to make a decision based on a condition’s value.

==

Test your knowledge Complete Self Review Exercises from your e-book, p Complete exercises from your Toolbox  Research exercise: 2, 3  Group work: 1, 2, 5, 10  Tutorial: 2, 3  Lab exercises: 1, 2, 3, 7