Java PPT OOPS prepared by Abhinav J.pptx

JainSaab2 26 views 20 slides Jul 12, 2024
Slide 1
Slide 1 of 20
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
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Java ppt


Slide Content

P r ep a r ed b y Abhinav Jain Neha Dubey Department Of CSE JAVA PROGRAMMING

OOPs (Object Oriented Programming System) Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or p a radi g m to de sign a p r o g ra m usi n g classe s a n d obje c t s . It simplifies the software development and maintenance by p r o viding some c o nc ep t s: Object Class Inheritance Polymorphism Abstraction Encapsulation

Characteristics of OOP Object Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be p h ysic a l and lo g ical. Class Collection of objects is called class. It is a logical entity. Inheritance When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Characteristics of OOP Polymorphism When one task is performed by different ways i.e. known as polymorphism . For example: to convense the customer differently, to draw something e.g. shape or r ec t an g l e et c . In java, we use method overloading and method overriding to a c hi e v e p o l ym o r phism. Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction.

Characteristics of OOP Encapsulation Binding (or wrapping) code and data together into a single unit is known as encapsulation . For example: c a ps u l e , it is wrap p ed with diff e r ent medici n e s . A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are p r i v a te her e .

What is difference between object-oriented programming language and object-based programming language? Object based programming language follows all the features of OOPs exce p t I n he r itan c e . J a v a S c r ipt and VBSc r ipt a r e examples of object based programming languages.

Naming convention Name Convention class name Should begin with uppercase letter and be a noun e.g. String ,System,Thread etc Inter f ace name Should begin with uppercase letter and be an adjective(wherever possible) e.g. Runnable(),ActionListener() etc method name Should begin with lowercase letter and be a verb . e.g. main(), print(), println(), a ctionListenerPerformed () V a r i a b le n ame Should begin with lowercase letter e.g. firstName,orderNumber etc Package name Sho u l d be in l o w er c as e let t er e . g . j a v a,lan g ,sql,util etc C on s t ants name S h o u l d be in u p pe r ca s e lette r . E. g . RE D ,YELL O W ,MAX_ P R I ORITY etc

Object An entity that has state and behavior is known as an object  An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class . For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behaviour . Ob j ec t is a n insta nce of a cla s s. Clas s is a t e mpla te or b lue p r int from which objects are created . So object is the instance(result) of a class.

Class A class is a group of objects that have common property. It is a template or blueprint from which objects are created.A class in java can contain: data member method constructor block A variable that is created inside the class but outside the method, is known as instance variable.Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why, it is kn o wn a s instance v a r ia b l e . Method In java, a method is like function i.e. used to expose behaviour of an object. Advantage of Method C ode Reusability C ode Op t imi z a tion

Class A class is a group of objects which have common properties. A class in Java can contain: Fields Methods Constructors Blocks

What are the different ways to create an object in Java? There are many ways to create an object in java. They are: By n ew k e y w ord By n e wIns t anc e () meth o d By cl o ne() meth o d By f a c t o r y meth o d e t c . new keyword The new keyword is used to allocate memory at runtime.

Annonymous object A n n o n ymou s si m p l y mea n s nam eles s .An o b ject t ha t h a v e no r efe r ence is known as annonymous object.If you have to use an object only once, ann o nymous ob ject is a good app r oa c h. class Calculation{ void fact( int n){ int fact=1; for ( int i=1;i<=n;i++){ fact=fact*i; } S y s t em.out. p r intl n ("fa c to r ial is "+fa c t ) ; } pu b l i c st a ti c v o i d mai n ( S t r ing args [ ] ){ n e w C alcul a tion() . f ac t (5) ; / / calling metho d with an n o n ymous object } }

Creating multiple objects by one type only W e c a n c r e a te m ul t iple o b jec t s b y o ne t y pe o n l y a s w e do in c a se of p r imiti v e s . Rectangle r1= new Rectangle(),r2= new Rectangle();//cre a ting t w o objects

Method overloading If a class have multiple methods by same name but different parameters, it is known as Method Overloading . If we have to perform only one operation, having same name of the methods increases the readability of the program. Advantage of method overloading? Method overloading increases the readability of the program . Different ways to overload the method Ther e a re t w o w a ys t o o v erlo a d the met hod in j a v aBy c han ging n u mbe r of ar g umen ts By c han ging the d a ta type

W h y M e t h od O v e r loadin g i s n o t p os sible b y c h a ngi n g t h e r eturn t y p e o f m e t hod? In java, method overloading is not possible by changing the r et u r n t y pe of the met h od bec aus e the r e ma y oc c u r ambig u it y . Let's see how ambiguity may occur: because there was problem: class Calculation{ int sum( int a, int b){System.out.println(a+b);} double sum( int a, int b){System.out.println(a+b);} p u b l i c st a ti c v o id mai n (St r ing arg s []){ Calcul a tion ob j = n e w Calcul a tion( ); int result=obj.sum(20,20); //Compile Time Error } }

C a n w e o v e r l oad m a i n () m e t hod? Yes, by method overloading.You can have any number of main methods in a class by method overloading. Let's see the simple example: class Simple{ public static void main( int a){ System.out.println(a); } pu b l i c st a ti c v oid main( S t r ing args [ ] ){ S y stem . o u t . p r intl n ("main() metho d i n v o k ed"); main(10); } }

Constructor Constructor is a special type of method that is used to in i tialize the ob ject. Constructor is invoked at the time of object creation . It constructs the values i.e. provides data for the object that is why it is k n o wn a s con st r ucto r . There are basically two rules defined for the constructor. Constructor name must be same as its class name Constructor must have no explicit return type

Constructor What is the purpose of default constructor? Default constructor provides the default values to the object li k e 0, n u l l et c . de p en d ing on the t y p e . A constructor that have parameters is known as parameterized constructor. Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects.

Constructor Overloading class Student{ int id; S t r i ng na me; int age; S tuden t ( int i ,St r i ng n) { i d = i ; na m e = n; } S tudent ( int i , S t r i ng n , int a){ i d = i ; na m e = n; age=a; } void display(){System.out.println(id+" "+name+" " +age);} pu b lic s t a t i c v oid main( S t r ing a r g s []){ St ude n t s 1 = n e w St ude n t(1 1 1,“ T a r if " ); Student s2 = new Student(222,“Soheab",25); s1.display(); s2.display(); } } Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by ta k ing i nt o a c c o u n t th e n umbe r of p a r amet e r s in t h e lis t an d th eir t y p e .

W h a t i s t h e d i f f er e nce be t w e e n co ns t ru c t or a nd m et h od ? Constructor Method Constructor is used to initialize the state of a n object. Me t h o d is used to ex p ose beh a vio u r of an object. Cons t r u c tor m ust n o t h a v e r et u r n t y p e . Me t h o d m ust h a v e r et u r n t y p e . Constructor is invoked implicitly. Me t h o d is i n v o k ed ex p lic i t l y . T he j a v a c o m piler pr o vides a de f a u lt c o ns t r u c tor if y ou d o n ’ t h a v e a n y constructor. Me t h o d is n o t pr o vided b y com p iler in a n y case. Constructor name must be same as the class name. Me t h o d n a m e m a y or m a y not be same as class n a m e .
Tags