Objects and Classes in JAVA introduction

RadhikaR7 31 views 11 slides May 01, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Introduction to Java


Slide Content

Objects and Classes in JAVA

OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world. For example, a student, a desk, a circle, a button can all be viewed as objects.

Objects will have state and behavior The state of an object (also known as its properties or attributes ) is represented by data fields with their current values. EG: A rectangle object has the data fields width and height , which are the properties that characterize a rectangle

The behavior of an object (also known as its actions ) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define methods named getArea () and getPerimeter () for circle objects.

An object is an instance of a class . You can create many instances of a class. Creating an instance is referred to as instantiation .

Classes Classes are constructs that define objects of the same type.

A class defines a kind of objects: specifies the kinds of attributes ( data ) an object of the class can have. provides methods specifying the actions an object of the class can take. An object is an instance of the class. Person is a class Alice and Bob are objects of the Person class.

What does a class have? Members of a class: Attributes (instance variables, data) For each instance of the class (object), values of attributes can vary, hence instance variables Methods Person class Attributes: name, address, phone number Methods: change address, change phone number

Example class Class student { int id; String name; Public static void main(String args []) { student s1=new student(); System.out.println (s1.id); System.out.println (s1.name); } }

Class student { int id; String name; } Class test1 { public static void main(String args []) { s tudent s1=new student(); S1.id=100; S1.name=“ abc ”; System.out.println (“s1.id+ “ “+s1.name+” “); } }

Class student { Int rollno ; String name; Void read( int r,String n) { rollno =r; name=n; } Void display() { System.out.println ( rollno +” “+name); } } Class student1 { public static void main(String args []) { student s1=new student(); Student s2=new student(); S1.read(100,”Karan”); S2.read(200,”Aryan”); S1.display(); S2.display() } }
Tags