Docsity

Object-Oriented Programming with Java: Concepts, Classes, and Methods, Study notes of Java Programming

Toggle sidebar
bg1
GOVINDRAO WANJARI COLLEGE OF ENGINEERING & TECHNOLOGY
148,149 SALAI GODHANI, HUDKESWAR ROAD, NAGPUR
DEPARTMENT OF INFORMATION TECHNOLOGY
Sub Code: 24AF1246PC304 Subject: OOPs using JAVA Sem: III rd
UNIT I
introduction to Java Applications: Introduction, Java Class Libraries, Typical Java Development
Environment, Memory Concepts, Arithmetic. Introduction to Classes and Objects: Introduction, Classes,
Objects, Methods and Instance Variables, Declaring a Class with a Method and Instantiating an Object of
a Class, Declaring a Method, Instance variables, set Methods and get Methods, Primitive Types vs.
Reference type double Types ,Initializing Objects with Constructors, floating point numbers.
What is Java?
Java is a popular high-level,object-oriented programming language that was
originally developed by Sun Microsystems and released in 1995. Currently, Java
is owned by Oracle, and more than 3 billion devices run Java. Java runs on a
variety of platforms, such as Windows, Mac OS, and the various versions
ofUNIX. Today Java is being used to develop numerous types of software
applications, including desktop apps, mobile apps, web apps, games, and much
more.
Java is a general-purpose programming language intended to let
programmers Write Once, Run Anywhere (WORA). This means that compiled Java
code can run on all platforms that support Java without the need to recompile.
Java - Overview
Java programming language was originally developed by Sun Microsystems, which was initiated by
James Gosling and released in 1995 as a core component of Sun Microsystems' Java platform (Java 1.0
[J2SE]).
The latest release of the Java Standard Edition is Java SE 23. With the advancement of Java and its
widespread popularity, multiple configurations were built to suit various types of platforms. For
example: J2EE for Enterprise Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE, and Java ME, respectively. Java is guaranteed to
be Write Once, Run Anywhere.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33

Partial preview of the text

Download Object-Oriented Programming with Java: Concepts, Classes, and Methods and more Study notes Java Programming in PDF only on Docsity!

GOVINDRAO WANJARI COLLEGE OF ENGINEERING & TECHNOLOGY

148,149 SALAI GODHANI, HUDKESWAR ROAD, NAGPUR

DEPARTMENT OF INFORMATION TECHNOLOGY

Sub Code: 24AF1246PC304 Subject: OOPs using JAVA Sem: III rd

UNIT I

introduction to Java Applications: Introduction, Java Class Libraries, Typical Java Development Environment, Memory Concepts, Arithmetic. Introduction to Classes and Objects: Introduction, Classes, Objects, Methods and Instance Variables, Declaring a Class with a Method and Instantiating an Object of a Class, Declaring a Method, Instance variables, set Methods and get Methods, Primitive Types vs. Reference type double Types ,Initializing Objects with Constructors, floating point numbers.

What is Java?

Java is a popular high-level,object-oriented programming language that was originally developed by Sun Microsystems and released in 1995. Currently, Java is owned by Oracle, and more than 3 billion devices run Java. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions ofUNIX. Today Java is being used to develop numerous types of software applications, including desktop apps, mobile apps, web apps, games, and much more. Java is a general-purpose programming language intended to let programmers Write Once, Run Anywhere (WORA). This means that compiled Java code can run on all platforms that support Java without the need to recompile.

Java - Overview

Java programming language was originally developed by Sun Microsystems, which was initiated by James Gosling and released in 1995 as a core component of Sun Microsystems' Java platform (Java 1. [J2SE]). The latest release of the Java Standard Edition is Java SE 23. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications. The new J2 versions were renamed as Java SE, Java EE, and Java ME, respectively. Java is guaranteed to be Write Once, Run Anywhere.

Java is āˆ’ Object-Oriented: In Java, everything is an object. Java can be easily extended since it is based on the object model. Platform Independent: Unlike many other programming languages, including C and C++, when Java is compiled, it is not compiled into a platform-specific machine but rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the virtual machine (JVM) on whichever platform it is being run on. Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master. Secure: With Java's secure feature, it enables the development of virus-free, tamper-free systems. Authentication techniques are based on public-key encryption. Architecture-neutral: The Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors with the presence of the Java runtime system. Portable: Being architecture-neutral and having no implementation-dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset. Robust: Java makes an effort to eliminate error-prone situations by emphasizing mainly compile-time error checking and runtime checking. Multithreaded: With Java's multithreaded feature, it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly. Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process. High Performance: With the use of just-in-Time compilers, Java enables high performance. Distributed: Java is designed for the distributed environment of the internet. Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry an extensive amount of run-time information that can be used to verify and resolve accesses to objects in run-time. History of Java James Gosling initiated Java language project in June 1991 for use in one of his many set-top box projects. The language, initially called 'Oak' after an oak tree that stood outside Gosling's office, also went by the name 'Green' and ended up later being renamed as Java, from a list of random words.

Frames Representation of objects Floating-point arithmetic Special methods Exceptions Instruction set summary Class libraries Public design, private implementation Java - OOPs (Object-Oriented Programming) Concepts

OOPs (Object-Oriented Programming System)

Object means a real-world entity such as a mobile, book, table, computer, watch, etc. Object- Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts. In this tutorial, we will learn about the concepts of Java (OOPs) object-oriented programming systems.

Java OOPs (Object-Oriented Programming) Concepts ļ‚· Class ļ‚· Object ļ‚· Inheritance ļ‚· Polymorphism ļ‚· Abstraction ļ‚· Encapsulation Class

Encapsulation

In an object-oriented approach, encapsulation is a process of binding the data members (attributes) and methods together. The encapsulation restricts direct access to important data. The best example of the encapsulation concept is making a class where the data members are private and methods are public to access through an object. In this case, only methods can access those private data. Java operators Java operators are the symbols that are used to perform various operations on variables and values. By using these operators, we can perform operations like addition, subtraction, checking less than or greater than, etc. There are different types of operators in Java, we have listed them below āˆ’ ļ‚· Arithmetic Operators ļ‚· Assignment Operators ļ‚· Relational Operators ļ‚· Logical Operators ļ‚· Bitwise Operators ļ‚· Misc Operators Arithmetic Operators Arithmetic operators are used to perform common mathematical operations. Operator Name Description Example Try it

  • Addition Adds together two values x + y
  • Subtraction Subtracts one value from another x - y
  • Multiplication Multiplies two values x * y / Division Divides one value by another x / y % Modulus Returns the division remainder x % y

++ Increment Increases the value of a variable by 1 ++x -- Decrement Decreases the value of a variable by 1 --x Example: public class ArithmeticExample { public static void main(String[] args) { int a = 10, b = 5; // Addition System.out.println("a + b = " + (a + b)); // 15 // Subtraction System.out.println("a - b = " + (a - b)); // 5 // Multiplication System.out.println("a * b = " + (a * b)); // 50 // Division System.out.println("a / b = " + (a / b)); // 2 // Modulus System.out.println("a % b = " + (a % b)); // 0 } }

Output:

a + b = 15

&= x &= 3 x = x & 3 |= x |= 3 x = x | 3 ^= x ^= 3 x = x ^ 3

= x >>= 3 x = x >> 3 <<= x <<= 3 x = x << 3 Example: public class AssignmentExample { public static void main(String[] args) { int a = 10; // Assign and add a += 5; System.out.println("a += 5: " + a); // 15 // Assign and subtract a - = 3; System.out.println("a - = 3: " + a); // 12 // Assign and multiply

a *= 2; System.out.println("a *= 2: " + a); // 24 // Assign and divide a /= 4; System.out.println("a /= 4: " + a); // 6 // Assign and modulus a %= 5; System.out.println("a %= 5: " + a); // 1 } } OUTPUT: When the above code is compiled and executed, it produces the following result āˆ’ a += 5: 15 a - = 3: 12 a *= 2: 24 a /= 4: 6 a %= 5: 1

Java Logical Operators

Logical operators are used to perform logical operations on boolean values. These operators are commonly used in decision-making statements such as if conditions and loops to control program flow. Operator Name Description Example Try it

!A: false !B: true

Java - What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented programming. Look at the following illustration to see the difference between class and objects: So, a class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the variables and methods from the class.

What are Java Classes?

A class is a blueprint from which individual objects are created (or, we can say a class is a data type of an object type). In Java, everything is related to classes and objects. Each class has its methods and attributes that can be accessed and manipulated through the objects. For example, if you want to create a class for students. In that case, "Student" will be a class, and student records (like student1, student2, etc) will be objects. We can also consider that class is a factory (user-defined blueprint) to produce objects. Properties of Java Classes A class does not take any byte of memory. A class is just like a real-world entity, but it is not a real-world entity. It's a blueprint where we specify the functionalities. A class contains mainly two things: Methods and Data Members. A class can also be a nested class. Classes follow all of the rules of OOPs such as inheritance, encapsulation, abstraction, etc.

Types of Class Variables

A class can contain any of the following variable types. Local variables āˆ’ Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Instance variables āˆ’ Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables āˆ’ Class variables are variables declared within a class, outside any method, with the static keyword.

Creating (Declaring) a Java Class

What are Java Objects?

An object is a variable of the type class, it is a basic component of an object-oriented programming system. A class has the methods and data members (attributes), these methods and data members are accessed through an object. Thus, an object is an instance of a class. If we consider the real world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior. If we consider a dog, then its state is - name, breed, and color, and the behavior is - barking, wagging the tail, and running. If you compare the software object with a real-world object, they have very similar characteristics. Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods. So, in software development, methods operate on the internal state of an object, and the object-to-object communication is done via methods. Creating (Declaring) a Java Object As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects. There are three steps when creating an object from a class āˆ’ Declaration āˆ’ A variable declaration with a variable name with an object type. Instantiation āˆ’ The 'new' keyword is used to create the object. Initialization āˆ’ The 'new' keyword is followed by a call to a constructor. This call initializes the new object. Syntax to Create a Java Object Consider the below syntax to create an object of the class in Java:

Class_name object_name = new Class_name([parameters]); Note: parameters are optional and can be used while you're using constructors in the class. Example to Create a Java Object In this example, we are creating an object named obj of Dog class and accessing its methods. // Creating a Java class class Dog { // Declaring and initializing the attributes String breed; int age; String color; // methods to set breed, age, and color of the dog public void setBreed(String breed) { this.breed = breed; } public void setAge(int age) { this.age = age; } public void setColor(String color) { this.color = color; } // method to print all three values public void printDetails() { System.out.println("Dog detials:"); System.out.println(this.breed);

/* First create an object / ObjectReference = new Constructor(); / Now call a variable as follows / ObjectReference.variableName; / Now you can call a class method as follows */ ObjectReference.MethodName(); Example In this example, We've created a class named Puppy. In Puppy class constructor, puppy name is printed so that when the object is created, its name is printed. An instance variable puppyAge is added and using getter/setter method, we can manipulate the age. In main method, an object is created using new operator. Age is updated using setAge() method and using getAge(), the age is printed. public class Puppy { int puppyAge; public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Name chosen is :" + name ); } public void setAge( int age ) { puppyAge = age; } public int getAge( ) { System.out.println("Puppy's age is :" + puppyAge ); return puppyAge;

public static void main(String []args) { /* Object creation / Puppy myPuppy = new Puppy( "tommy" ); / Call class method to set puppy's age / myPuppy.setAge( 2 ); / Call another class method to get puppy's age / myPuppy.getAge( ); / You can access instance variable as follows as well */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } } Output If we compile and run the above program, then it will produce the following result āˆ’ Name chosen is :tommy Puppy's age is : Variable Value :

Java Class Attributes

Java class attributes are the variables that are bound in a class i.e., the variables which are used to define a class are class attributes. A class attribute defines the state of the class during program execution. A class attribute is accessible within class methods by default.