1 / 130

CLASS

CLASS. A Class represents a template for several objects that have common properties A class defines all the properties common to the object – attributes and methods. A Class is a set of attributes and operations that are performed on the attributes.

bjanes
Download Presentation

CLASS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CLASS • A Class represents a template for several objects that have common properties • A class defines all the properties common to the object – attributes and methods. • A Class is a set of attributes and operations that are performed on the attributes. • A Class is the blueprint from which individual objects are created. • An object is an instance of a class

  2. Class Declaration

  3. CLASS Example class Account { private String accountName; private double accountBalance; public withdraw(); public deposit(); public determineBalance() }

  4. OBJECTS • Object-oriented is a way of looking at a Software System as a collection of interactive objects • Object is an instance of a class • Object represents an entity either physically, conceptual, or software.

  5. OBJECTS • All the objects share the same attribute names and methods with other objects of the same class. • Each object has its own value for each of the attribute.

  6. Example of Objects

  7. CLASSES / OBJECTS

  8. OBJECTS

  9. What goes inside a class

  10. ADDING METHODS • A Class with only data fields has no life. Objects created by such a class cannot respond to any messages. • Methods are declared inside the body of the class but immediately after the declaration of data fields. The general form of a method declaration is: type methodName (Paramter-list) { method-body; }

  11. Method Declaration

  12. ADDING METHODS TO CLASS CIRCLE

  13. CREATING OBJECTS OF A CLASS

  14. CREATING OBJECTS OF A CLASS

  15. Accessing Object Data

  16. EXECUTING METHODS ON OBJECT

  17. BASIC PRINCIPLES OF OO Encapsulation Data Abstraction OOP Paradigm Inheritance Polymorphism

  18. Encapsulation • It associates the code and the data it manipulates into a single unit; and keeps them safe from external interference and misuse Encapsulation Data Abstraction OOP Paradigm Inheritance Polymorphism

  19. ENCAPSULATION • All information (attributes and methods) in an object oriented system are stored within the object/class. • Information can be manipulated through operations performed on the object/class – interface to the class. Implementation is hidden from the user. • Object support Information Hiding – Some attributes and methods can be hidden from the user

  20. ENCAPSULATION - Example

  21. Without Encapsulation?

  22. With Encapsulation

  23. INHERITANCE • New data types (classes) can be defined as extensions to previously defined type. • Parent Class ( Super Class ) –Child Class ( Sub Class) • Subclass inherits properties from the class Encapsulation OOP Paradigm Data Abstraction Inheritance Polymorphism

  24. INHERITANCE - Example

  25. INHERITANCE - Example

  26. INHERITANCE - Example

  27. ABSTRACTION • Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and methods that operate on these attributes. Encapsulation Data Abstraction OOP Paradigm Inheritance Polymorphism

  28. Polymorphism • An Object of type circle or rectangle can be assigned to a Shape object. The behavior of the object will depend on the object passed. Encapsulation Data Abstraction OOP Paradigm Inheritance Polymorphism

  29. POLYMORPHISM – Method Overloading

  30. CONSTRUCTOR • Initializing all the variables in a class is once after the instance is created. • Automatic initialization is performed through the use of a constructor. • A constructor initializes an object immediately upon creation. • It has the same name as the class in which it resides and is syntactically similar to a method. • Once defined, the constructor is automatically called immediately after the object is created, before the “new” operator completes. • It doesn’t have return type, not even void, because of implicit return type of a class. Constructor types • Default Constructor • Parameterless Constructor • Parameterized Constructor

  31. CONSTRUCTOR class Account { private int id; private double balance; public Account() {} public Account( int a, double b) { id = a; balance = b; } public Account( Account ac) { id = ac.id; balance= ac.balance; } } • Initialize state of the object • Special method have same name as that of class name • Can’t return anything • Doesn’t have return type • Can only be called once for an object • Can be private • Can’t be static • Can overloaded but can’t overridden • Can be • Default, parameterized, and Copy constructor Default constructor Parameterized constructor Copy constructor

  32. Default Constructor Example Class DemoBox { public static void main(String args[]) { double vol; // Calls Constructor automatically Box b1 = new Box(); vol = b1.volume(); System.out.println(“Volume is:”+vol); } } O/P: Volume is 1000.0 Class Box { double width; double height; double depth; //Constructor Definition Begins Box() { width = 10; height = 10; depth = 10; } //Member Function double volume() { return width*height*depth; } }

  33. Parameterized Constructor: Class DemoBox { public static void main(String args[]) { double vol; // Calls Constructor automatically Box b1 = new Box(10,10,10); b1.display(); vol = b1.volume(); System.out.println(“Volume is:”+vol); } } O/P: Parameterized Constructor Width is:10 Height is:10 Depth is:10 Volume is 1000.0 Class Box { double width, height, depth; //Constructor having arguments Box(double w,double h, double d) { width = w; height = h; depth = d; } void display() { System.out.println(“Width is: “+ width); System.out.println(“Height is: “+ height); System.out.println(“Depth is: “+ depth); } //Member Function double volume() { return width*height*depth; } }

  34. Copy Constructor Box(Box b) { width=b.width; height=b.height; depth=b.depth; } } class ObjectConst { public static void main(String args[]) { double vol; Box b1 = new Box(); vol = b1.volume(); Box b2 =new Box(b1); System.out.println("volume " +b1.volume()); } } class Box { double width; double height; double depth; //Constructor Definition Begins Box() { width = 10; height = 10; depth = 10; } //Member Function double volume() { return width*height*depth; } Copying the content of one object to another object by passing the object to the constructor parameter. Syntax:Classname(ClassnameObj)

  35. Constructor Overloading // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; } } class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; • As-like method overloading, constructor can also overloaded. • A class may have more than one constructor definitions, but having difference in their signature i.e.. Number of arguments class Box { double width, height, depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box }

  36. Constructor Overloading // get volume of first box vol= mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); } }

  37. Using Objects as Parameters class Test { inta, b; Test(inti, int j) { a = i; b = j; } // return true if o is equal to the invoking object booleanequals(Test o) { if(o.a == a && o.b == b) return true; else return false; } } class PassOb { public static void main(String args[]) { Test ob1 = new Test(100, 22); Test ob2 = new Test(100, 22); System.out.println("ob1 == ob2: " + ob1.equals(ob2)); } }

  38. Returning Objects // Returning an object. class Test { int a; Test(int i) { a = i; } Test incrByTen() { Test temp = new Test(a+10); // return new Test(a +10); return temp; } } class RetOb { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); ob2 = ob2.incrByTen(); System.out.println("ob2.a after second increase: “ + ob2.a); } }

  39. Access Modifiers Controlling the parts of a program that can access by the members. By controlling access, we can prevent misuse.-> Black box, which may be used, but the inner working of which are not open. Java’s access specifiers are :

  40. Access Specifier

  41. class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error // ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a+ " " + ob.b + " " + ob.getc()); } } class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) // set c's value { c = i; } int getc() // get c's value { return c; } }

  42. PACKAGES • Packages are java’s way of grouping a variety of classes and / or interfaces together. The grouping is usually done according to functionality. In fact, packages act as “containers” for classes. What are the benefits of Packages? • What are the benefits of Package? • The classes contained in the packages can be easily reused. • In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name. They may be referred by their fully qualified name, comprising the package name and class name. • Package provide a way to “hide classes”thus preventing other programs or package from accessing classes that are meant for internal use only. • Packages also provide a way for separating “design” from “coding”. First we can design classes and decide their relationships, and then we can implement the java code needed for the methods. It is possible to change the implementation of any method without affecting the rest of the design.

  43. Types of Packages Java packages are therefore classified into two types. 1. Pre – defined packages (Java API Packages) 2. User – defined packages • Java API Packages

  44. 2. USER DEFINED PACKAGES How to Creating our own Packages Creating our own package involves the following steps: 1. Declare the package at the beginning of a file using the form package package_name; 2. Define the class that is to be put in the package and declare it public. 3. Create a subdirectory under the directory where the main source files are stored. 4. Save the Package file in .java compile the file creates .class file in the subdirectory. 5. The subdirectory name must match the package name exactly. Note: Java also supports the concept of package hierarchy. This done by specifying multiple names in a package statement, separated by dots. Example: package firstPackage.secondpackage;

  45. Example: Create Package: package package1; public class ClassA { public void displayA() { System.out.println("Class A"); } } How to import the package: import package1.ClassA; class PackageTest1 { public static void main(String args[]) { ClassAobjectA = new ClassA() objectA.displayA(); } }

  46. ACCESS PROTECTION

  47. Example program for the above Access Protection Tabular Protection.java: package p1; public class Protection { int n = 1; private int n_pri = 2; protected int n_pro = 3; public int n_pub = 4; public Protection() { System.out.println("base constructor"); System.out.println("n = " + n); System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }

  48. This is file Derived.java: package p1; public class Derived_Protextends Protection { public Derived() { System.out.println("derived constructor"); System.out.println("n = " + n); //System.out.println("n_pri = " + n_pri); //Cannot Access because //accessing level is same class //in same package System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }

  49. This is file SamePackage.java: package p1; public class SamePackage { public SamePackage() { Protection p = new Protection(); System.out.println("same package constructor"); System.out.println("n = " + p.n); //System.out.println("n_pri = " + p.n_pri); //Cannot Access because //accessing level is same //class in same package System.out.println("n_pro = " + p.n_pro); System.out.println("n_pub = " + p.n_pub); } }

More Related