0% found this document useful (0 votes)
41 views8 pages

Practical Assignment-2

The document provides an overview of Java classes and objects, explaining their roles in Object-Oriented Programming (OOP). It details the structure of class declarations, the properties of classes, and how to create and manipulate objects within Java programs. Additionally, it includes examples of class implementations and exercises for further practice.

Uploaded by

shraddha30405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Practical Assignment-2

The document provides an overview of Java classes and objects, explaining their roles in Object-Oriented Programming (OOP). It details the structure of class declarations, the properties of classes, and how to create and manipulate objects within Java programs. Additionally, it includes examples of class implementations and exercises for further practice.

Uploaded by

shraddha30405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Experiment No: 2 JAVA program to implement class mechanism. Create a class, methods and invoke them inside main method In Java, classes and objects are basic concepts of Object-Oriented Programming (OOPs) that are used to represent real-world concepts and entities, The class represents a group of objects having similar properties and behavior. Java Classes A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes, It is a user-defined blueprint or prototype from which objects are created, For example, Student is a class while a particular student named Ravi is an object. Properties of Java Classes 1. Class is not a real-world entity, It is just a template or blueprint or prototype from which objects are created, 2. Class does not occupy memory. 3. Class is a group of variables of different data types and a group of methods. 4. AClass in Java can contain: + Data member + Method + Constructor + Nested Class = Interface Lab Manual: OBJECT ORIENTED METHODOLOGY Class Declaration in Java access. modifier class { data member; method; constructor; nested class; interface; } Components of Java Classes In general, class declarations can include these components, in order: Modifiers: A class can be public or has default access (Refer this for details). 2. Glass keyword: class keyword is used to create a class. 3. Class nam the name should begin with an initial letter (capitalized by convention). 4, Superclass(if any): The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. 5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. 6. Body: The class body is surrounded by braces, { }. Lab Manual: OBJECT ORIENTED METHODOLOGY Java Objects An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities, Objects are the instances of a class that are created to use the attributes and methods of aclass. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists, of: 1. State: It is represented by attributes of an object, Italso reflects the properties of an object. 2. Behavior: It is represented by the methods of an object. It also reflects the response of an object other objects. 3. Identity: It gives a unique name to an object and enables one object to interact with other objects. Objects: Real World Examples Pencil Apple Book & Bag Board Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, and “menu’, An online shopping system might have objects such as “shopping cart’, “customer”, and “product” Create an Object In Java, an object is created from a class. We have already created the class named Main, so now we can use this to create objects. Lab Manual: OBJECT ORIENTED METHODOLOGY To create an object of Main, specify the class name, followed by the object name, and use the keyword new: Example Create an object called "myObj" and print the value of x: public class Main { intx=5; public static void main(String{] args) { Main myObj = new Main); ‘System.out printIn(myObj.x); } } Object and Class Example: main within the class In this example, we have created a Student class which has two data members id and name. We are creating the object of the Student class by new keyword and printing the object's value. Here, we are creating a main() method inside the class. File: Student java //Java Program to illustrate how to define a class and fields //Defining a Student class. class Student{ //defining fields ;//field or data member or instance variable Lab Manual: OBJECT ORIENTED METHODOLOGY String name; //creating main method inside the Student class public static void main(String args[]){ //Creating an object or instance Student s1=new Student();//creating an object of Student //Printing values of the object System.out printIn(s1.id);//accessing member through reference variable System.out printIn(si.name); Example- 1, Object and Class Example: Employee Let's see an example where we are maintaining records of employees. File: TestEmployee,java class Employee int ic String name; float salary; void insert(int i, String n, float s) { id=i; name=n; salary=s; + Lab Manual: OBJECT ORIENTED METHODOLOGY void display(){System.out printin(id+" "+name+" “+salary);} } public class TestEmployee { public static void main(String[] args) { Employee e1=new Employee(); Employee e2=new Employee(); Employee e3=new Employee(); el insert(101,"ajeet" 45000); e2.insert(102,"irfan",25000); 3 insert(103,"nakul",55000); el.displayQ); e2.display(; e3.display(Q); + Output: 101 ajeet 45000.0 102 irfan 2500.0 103 nakul 5500.0 Creating multiple objects by one type only Real World Example: Account File: TestAccountjava //Java Program to demonstrate the working of a banking-system //where we deposit and withdraw amount from our account. //Creating an Account class which has deposit() and withdraw() methods class Account{ ‘int acc_no; String name; float amount; //Methos to initialize object void insert(int a String n,float amt){ ace_n Lab Manual: OBJECT ORIENTED METHODOLOGY name=n; amount=amt; } //deposit method void deposit(float amt){ amount=amount+amt; System.out printIn(amt+" deposited” } //withdraw method void withdraw(float amt}{ if(amount

You might also like