Classes, Interfaces and Packages

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Final and Abstract Classes
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
By Waqas The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class.
Programming in Java CSCI-2220 Object Oriented Programming.
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 COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© 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)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Java Programming Language
Final and Abstract Classes
C++ Object Oriented 1.
Presentation transcript:

Classes, Interfaces and Packages

Objectives After completing this chapter, the student will be able to: Explain Java classes Explain Java objects Declare and define simple Java classes Create objects of Java classes and do manipulation on those objects by invoking class methods Explain class constructors and finalizers Explain access modifiers and the difference between different access modifiers

Objectives contd.. Explain class inheritance Explain the difference between method overloading and method overriding Explain abstract classes, interfaces and interface implementation Explain Java packages Explain CLASSPATH environment variable and set it to suit his/her requirement Write programs to create Java packages Explain the usage of import and package statements Write Java programs using previously created packages Explain the important packages in Java API

Introduction Java is an object oriented programming language. Object oriented programming revolves around the concept of objects. Objects are software bundles of data abstractions and procedures that act on the data. These procedures are also known as methods. Two powerful benefits of encapsulation are implementation hiding and modularity.

Classes A class is a template or a prototype that defines a type of object. A class is to an object what a blue print is to a house. A class outlines the properties of an object. Objects created from the same class show similar characteristics.

Declaring Classes The syntax for declaring classes in Java is as follows: class identifier { classBody } ClassBody consists of data declarations and method definitions. Curly braces surround the classBody.

The new Operator Here is how you create instances of classes or object variables and invoke methods or member functions in Java. ExampleClass ec; ec = new ExampleClass(); ec.exampleMethod1(); ec.exampleMethod2(); The declaration “ExampleClass ec:” simply states what type of object variable ec will be. The object is actually created when the new operator is called.

Constructors Constructors are special methods you can implement in your classes. Constructors are always given the same name as that of the class. It do not have return type. Used to initialize the member variables.

Finalize Method Finalizer methods are automatically called when an object is destroyed. Java objects are destroyed during garbage collection. Finalizers are thus called when Java performs garbage collection. void finalize() { // do clean u job }

Java Objects as Class Member Variables Java class member variables are of two types. One is the built in type like char, int, float, double, etc. Class member variables can be Java objects as well. Ex: String msg;

Method Overloading If two or more methods of a class have the same name but different signatures, then the method name is said to be overloaded. The signature of a method consists of the name of the method and the number and types of formal parameters in particular order. Only the method name is reused in overloading, so method overloading is actually method name overloading. Overloaded methods may have arguments with different types and order of the arguments may be different.

Method Overloading contd.. Overloaded methods are not required to have the same return type or the list of thrown exceptions. Overloading is used while implementing several methods that implement similar behavior but for different data types. Overloaded methods are independent methods of a class and can call each other just like any other method.

Constructor Overloading Constructors can also be overloaded as they are also methods. Java has some classes with overloaded constructors. Ex: Integer i =new Integer(3); Integer j =new Integer(“2012”); One constructor can call another overloaded constructor of the same class by using this keyword. One constructor can call constructor of it’s super class by using the super keyword.

Deriving Classes A Class can be built on another class that is already defined and is existing. This already existing class is called the base class or the parent class. The newly built class is called the derived class or child class. extends is the keyword used for inheritance. The Child class inherits all the properties of the parent class. The child class inherits all the member variables and methods of the parent class. The child class can have its own member variables and methods.

Method Overriding When a class defines a method with same method name ,argument types, argument order and return type as a method in its super class, it’s called method overriding. The method in the super class is said to be overridden by the method in the subclass. Overriding method actually replaces the behavior in super class for a subclass. The call super.method() will invoke the method of immediate super class.

Rules for Method Overriding Methods overriding cannot be declared more private than the super class method. Any exceptions declared in overriding method must be of the same type as those thrown by the super class or a subclass of that type. Methods declared as final cannot be overridden. An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden. Methods declared as private cannot be overridden.

Access Modifiers Access to member variables and methods in a Java class is accomplished through access modifiers. Access modifiers define the varying levels of access between class members and outside world. Access modifiers are declared immediately before the data type of a member variable or the return type of a method. There are four access modifiers: default, private, protected and public.

Access Modifiers Access modifiers on class member declarations are listed here. Modifier Same Class Same Package Subclass Universe private Yes default protected public

The static Modifier The static modifier specifies that a variable or a method is the same for all objects of a particular class. static variables memory is allocated only once i.e. the first time when we create the object. static method can be accessed without having to create an object of the class. Static method has access to static variables only.

The final Modifier The final modifier with variable specifies that the variable has a constant value. The final modifier with method specifies that the method can not be overridden in a subclass or child class. The final modifier with class specifies that the class can not be inherited.

The native Modifier The native modifier informs the Java compiler that a method’s implementation is in an external C file. Syntax: native void classMethod();

Abstract Classes and Methods Abstract classes are classes that are partially implemented. Abstract methods are methods that are declared, but not implemented. Abstract classes are used as base classes for deriving subclasses. In the child class, all the abstract methods of the base class will be overridden. Constructors, static methods and private methods cannot be abstract.

Interfaces An interface is a prototype for a class. The syntax for creating interfaces is shown below: interface identifier { interface Body } Here interface is a keyword. identifier is the name of the interface. interface body consists of static final variables and abstract methods.

Naming Conflicts A Java program consists of a collection of classes. When a Java program is compiled, each class in it is compiled into a byte code file with the class name as file name and extension .class. This creates a potential naming conflict. You should not have a second Java program in your working directory in which classes, with names the same as that of the classes in your first program, are appearing. It is always safe to have unique class names in every program in the same directory.

Points to note Any method in a class can happily reference another class or class method even if that class does not occur immediately above. You can not define a second class when you are already in the middle of defining another class. You cannot have class declaration in one file and class method implementation in a different file.

Large Programs Large program means a program consisting of more than one file. The Java programs we have seen so far consisted of one single compilation unit or file. The class you are compiling makes references to other classes in other files. Javac compiler will compile all these classes. If a class is modified after the previous compilation, Java compiler detects that and that class alone will be compiled in the next run.

Packages and Imports A package provides a means to group together related classes and reference them as a whole. To create a package you need to label each file in which the classes are defined with the name of the package. This is done through a statement package packname; as the very first statement of the file concerned. The import statement is used to import the classes in the named package into the file concerned. Syntax : import packagename.*; import packagename.Classname;

Classpath You will find some relationship between the CLASSPATH, package name and the directory path in which the file is located. The package name is the difference between the directory path and CLASSPATH. The package name bridges the gap in the directory path name between the CLASSPATH and the file name. CLASSPATH + packagename = DirectoryPath

javac -d option It is possible to segregate the .class files into different directories with –d option in javac command. Ex: Example1.java is in the package mypack.pack1. Compile as d:\user\javac –d c:\cmc Example1.java * c:\cmc directory should exist * The directory c:\cmc\mypack\pack1 will be created if it does not already exist. * Example1.class will be found in c:\cmc\mypack\pack1\Example1.class