Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.
Lecture 5: Interfaces.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
C++ Classes & Data Abstraction
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
UML Class Diagram: class Rectangle
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer 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.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
CITA 342 Section 1 Object Oriented Programming (OOP)
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Object-oriented Programming
NESTED CLASSES REFLECTION PROXIES.
Examples of Classes & Objects
Objects as a programming concept
CompSci 230 S Programming Techniques
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
UML Class Diagram: class Rectangle
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Nested class.
User-Defined Classes and ADTs
Object Based Programming
Corresponds with Chapter 7
Classes & Objects: Examples
Chapter 8: User-Defined Classes and ADTs
User-Defined Classes and ADTs
CISC124 Assignment 3 due tomorrow at 7pm.
Java Programming, Second Edition
Chapter 8 Classes User-Defined Classes and ADTs
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
By Rajanikanth B OOP Concepts By Rajanikanth B
Final and Abstract Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Introduction to classes Sangeetha Parthasarathy 06/11/2001

OOPs Concepts OOPs encapulates data(attributes) and methods (behaviours) into objects. Objects have the property of information hiding. This means that implementation details are hidden within the object themselves. Java programmers concentrate on creating their own user- defined types called classes. Classes are also referred as programmer-defined types.

Introduction to classes Each class contains data as well as the set of methods that manipulate the data. The data components of a class are called as instance variables. The methods or functions are called as interfaces.

Class Scope The class instance variables and methods belong to that classs scope. Outside a classs scope, the members are accessible not by class name but by the object name. The class members which are public are accessible by using the object name. The class members which are private are accessible by public methods.

Controlling access to members The are four member access modifiers: public, private, protected and default. Public modifier: The members with this specifier are accessible everywhere. Private modifier: The members with this specifier are accessible only within the class. Protected modifier: Member with this modifier are accessible in all the child classes in any package. Default: The member are accessible within the default package.

Access specifier of class There are two access specifiers for a class: Default – The specifier as default for a class is subclasses only for the classes within the same package. Public – This specifier is accessible anywhere.

Inner class The inner class are defined within the top-level class. The top-level class have rights to all the members of the inner class irrespective of the access specifier. The top-level class have rights to the class irrespective of the class being public, private, protected or default. The inner class can be static or final. The inner class members can be accessed by using the outer class object.inner class objectname.inner class variable name. A static inner class need not be instantiated. But an object should be specified.

The structure of the inner class is: public class outerclass {//Start of outerclass innerclass i //Data member of outerclass public class innerclass {//Start of innerclass int a=10; }//End of innerclass public static void main(String args[]) { outerclass o=new outerclass(); o.i.a=10; } }//End of outerclass

An example of inner class public class outerClass { // private innerClass x; The private innerClass methods can be accessed in main by using the outer class object //protected innerClass x; The private innerClass methods can be accessed in main by using the outer class object //public innerClass x; The public innerClass methods can be accessed in main by using the outer class object innerClass x;

public class innerClass { //private int a; The private innerClass data members can be accessed in main by using the outer class object //public int a; The public innerClass data members can be accessed in main by using the outer class object //protected int a; The protected innerClass data members can be accessed in main by using the outer class object private int a; //The default innerClass data members can be accessed in main by using the outer class object innerClass() { a=9; } public void print() { System.out.println("\n\n\t\tA in inner class is: "+a); }

outerClass() { x=new innerClass(); //x.a=10; - This is possible as outer class can have access to inner class //data members whether private, public or protected. } public static void main(String args[]) { outerClass o=new outerClass(); o.x.print(); o.x.a=10; o.x.print(); //x.a=10 - This is not possible as x is a non-static reference to a member //x.print(); - This is not possible as x is a non-static reference to a member }

Embedded Class A class can be embedded in another class. The structural diagram of an embedded class is: public class topClass { public static void main(String args[]) { } class embeddedClass { }

Embedded classes cannot be public, private or protected. It can only be default as there can be only one class with the access specifiers. Embedded class is local to the top-level class. If the class in which the embedded class exists is inherited from, the entire is the parent for the child class. A class should be embedded only if this is applicable to the top-level class.

An Embedded class example public class embedClass { innerClass i; public void print() { i.print(); } public static void main(String args[]) { embedClass o=new embedClass(); o.print(); }

class innerClass //Embedded class cannot be protected, private or public. //Only the class that contains main can have other access specifiers. { static int a=9; innerClass() { a=11; } public static void print() { System.out.println("\n\n\t\tA in inner class is: "+a); }

Anonymous class Anonymous class is the object of the class passed in the function. The structural diagram is: m.java public class m { }

m1.java public class m1 { public void print( m x) { } public static void main(String args[]) { m1 x1=new m1(); x1.print(new m()); }

Anonymous class can also be an embedded class in the top- level class.