Packages Inner Classes

Similar documents
6.Introducing Classes 9. Exceptions

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Object Orientation Fourth Story. Bok, Jong Soon

Another IS-A Relationship

Programming overview

Declaring Array Variable

Topic 6: Inner Classes

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Interfaces. An interface forms a contract between the object and the outside world.

Timing for Interfaces and Abstract Classes

CMSC 132: Object-Oriented Programming II

Declarations and Access Control SCJP tips

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

*An nested class is a class that is defined inside another class.

C30b: Inner Class, Anonymous Class, and Lambda Expression

Data Types, Literals, Operators

Interfaces (1/2) An interface forms a contract between the object and the outside world.

Android Debug Framework

Subtype Polymorphism

Swing: Building GUIs in Java

Java: introduction to object-oriented features

Introduction to Programming Using Java (98-388)

Swing: Building GUIs in Java

CMSC 132: Object-Oriented Programming II

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Date & Time Handling In JAVA

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Getting Started With Java

Java. Package, Interface & Excep2on

Exercise. Zheng-Liang Lu Java Programming 273 / 324

1 Shyam sir JAVA Notes

CITS2210 Object-Oriented Programming. Topic 10. Java: Nested and Inner Classes

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Chapter 15 Event-Driven Programming and Animations

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Object-Oriented Programming, Part 2

CS 251 Intermediate Programming More on classes

Chapter 4 Java Language Fundamentals

5. PACKAGES AND INTERFACES

OVERRIDING. 7/11/2015 Budditha Hettige 82

The Essence of OOP using Java, Nested Top-Level Classes. Preface

C08: Inheritance and Polymorphism

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Array. Prepared By - Rifat Shahriyar

What about Object-Oriented Languages?

Inheritance. Transitivity

Types of Views. View category Purpose Examples of views. Display a particular type of content, such as an image or text.

Chapter 14 Abstract Classes and Interfaces

EnclosingClass.StaticClass staticclass = new EnclosingClass.StaticClass(); System.out.println(staticClass.getName());

Access Restrictions of Methods/Fields

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Internationalization and Localization

Java Programming Training for Experienced Programmers (5 Days)

Reflection. Based on

The static keyword Used to denote fields and methods that belong to a class (but not to any particular object).

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Protection Levels and Constructors The 'const' Keyword

Chapter 6 Introduction to Defining Classes

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Chapter 13. Interfaces and Inner Classes

CS61B Lecture #13: Packages, Access, Etc.

CS61B Lecture #13: Packages, Access, Etc.

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

And Even More and More C++ Fundamentals of Computer Science

core Advanced Object-Oriented Programming in Java

Overview. Elements of Programming Languages. Objects. Self-Reference

Programming Languages and Techniques (CIS120)

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Class, Variable, Constructor, Object, Method Questions

Final Exam CS 251, Intermediate Programming December 10, 2014

CS260 Intro to Java & Android 03.Java Language Basics

Overview of Eclipse Lectures. Module Road Map

Lecture 02, Fall 2018 Friday September 7

CS 112 Programming 2. Lecture 14. Event-Driven Programming & Animations (1) Chapter 15 Event-Driven Programming and Animations

QUESTIONS FOR AVERAGE BLOOMERS

Constructors for classes

Today s Agenda. Quick Review

Tutorial 02: Writing Source Code

COMP6700/2140 Packages, Modules and Jigsaw

Java Programming Lecture 7

a guide to an object oriented programming language Niket Sheth JAVA

Vendor: Oracle. Exam Code: 1Z Exam Name: Java Certified Programmer. Version: Demo

CS 251 Intermediate Programming Inheritance

Exercise 15. Introduction to packages

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

Inheritance and Interfaces

Packages. Examples of package names: points java.lang com.sun.security drawing.figures

Fundamentals of the Java Programming Language

SCOPE. public class ABC { public static void main( String args [ ] ) { Not accessible outside the block. Local to this block; accessible within it

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CSCE3193: Programming Paradigms

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

Core Java Interview Questions and Answers.

Text Properties Data Validation Styles/Themes Material Design

Java Strings Java, winter semester

Transcription:

Packages Inner Classes Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283

Learning - Topics Packages Import Static Import Nested Class Static Nested Class Inner Class Member Inner Class Method Local Inner Class Anonymous Inner Class

Packages Packages are containers for classes. They are used to keep the class name space compartmentalized The package is both a naming and a visibility control mechanism The package name is the first statement in any java program If you omit the package statement, the class names are put into the default package, which has no name The general form of the package statement: package pkg;

Finding Packages How does the Java run-time system know where to look for packages that you create? First, by default, the Java run-time system uses the current working directory as its starting point. Thus, if your package is in a subdirectory of the current directory, it will be found. You can specify a directory path or paths by setting the CLASSPATH environmental variable. you can use the -classpath option with java and javac to specify the path to your classes.

Import Statement Java includes the import statement to bring certain classes, or entire packages, into visibility The general form of the import statement: import pkg1 [.pkg2].(classname *); In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. The basic language functions are stored in a package inside of the java package called java.lang, it is implicitly imported by the compiler for all programs.

Static Import Statement By following import with the keyword static, an import statement can be used to import the static members of a class or interface When using static import, it is possible to refer to static members directly by their names, without having to qualify them with the name of their class. This simplifies and shortens the syntax required to use a static member

Static Import Statement There are two general forms of the import static statement: Brings into view a single name : import static pkg.type-name.staticmember-name; Here, type-name is the name of a class or interface that contains the desired static member. Its full package name is specified by pkg. The name of the member is specified by static-member-name. The second form of static import imports all static members of a given class or interface. Its general form is shown here: import static pkg.type-name.*;

Nested Class The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here: class OuterClass {... class NestedClass {... } } A nested class is a member of its enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

Nested Class - Categories Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes class OuterClass {... static class StaticNestedClass {... } class InnerClass {... } } Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class

Inner class An inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself. There are three types of inner classes: Member Inner Class Anonymous Inner Class Local Inner Class

Nested Class Types

Member inner class A member class is defined at the top level of the class. It may have the same access modifiers as variables (public, protected, package, static, final), and is accessed in much the same way as variables of that class.

Instantiating an inner class Outside to outer class Outerclass.Innerclas inref =new Outerclass().new Innerclas() Within outer class Innerclas inref =new Innerclas();

Method local inner class A local inner class is defined within a method, and the usual scope rules apply to it. It is only accessible within that method, therefore access restrictions (public, protected, package) do not apply. The objects (and their methods) created from this class may not persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method.

anonymous inner class An anonymous inner class is one that is declared and used to create one object (typically as a parameter to a method), all within a single statement. It should be used to override method of class or interface An anonymous inner class may extend a class: new SuperClass(parameters){ class body }Here, SuperClass is not the name of the class being defined, but rather the name of the class being extended. The parameters are the parameters to the constructor for that superclass. An anonymous inner class may implement an interface: new Interface(){ class body }

Example Anonymous inner class class Myclass{ void go() { new Bar(){ //Extending Class void dosomething(){sop( dosomethings )} }.dosomething() ; // By Implementing interface new Foo(){ void foof(){sop( Foof )}}.foof(); } } interface Foo{ void foof(); } class Bar{ void dostuff(foo f){} }