MATRUSRI ENGINEERING COLLEGE�DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SUBJECT NAME: OOP USING JAVA
BE III SEM 2022-23
FACULTY NAME: A V MURALI KRISHNA
MATRUSRI
ENGINEERING COLLEGE
UNIT-1
MATRUSRI
ENGINEERING COLLEGE
Object Oriented Programming: Principles, Benefits Of Object Oriented Programming.
Introduction To Java: Java Buzzwords, Bytecode. Java Programming Fundamentals: Applet And Application Program Using Simple Java Program, Data Types, Variables, Arrays, Operators, Expressions, Control Statements, Type Conversion And Casting, Concepts Of Classes, Objects, Constructors, Methods, Access Control, This Keyword, Garbage Collection, Overloading Methods And Constructors, Introducing
Access Control, Static, Final, Nested And Inner Classes, Exploring String Class, Using Command-linear Arguments.Inheritance: Inheritance Concept, Types Of Inheritance, Member Access Rules, Use Of Super And Final. Polymorphism - Dynamic Binding, Method Overriding, Abstract Classes And Methods.
Syllabus
INTRODUCTION:��
PRELIMINARY CONCEPTS
MATRUSRI
ENGINEERING COLLEGE
OUTCOMES:
JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It is a simple programming language. Writing, compiling and debugging a program is easy in java. It helps to create modular programs and reusable code.
Course Objective: To understand fundamentals of object-oriented programming in Java which includes defining classes, invoking methods, difference between applet and application programs, using class libraries
Achieve proficiency in object-oriented concepts and also learns to incorporate the same into the Java programming language
CONTENTS:�1) Principles, Benefits Of Object Oriented Programming �2) Java Buzzwords, Bytecode. Java Programming Fundamentals: Applet And Application Program Using Simple Java Program, Data Types, Variables, Arrays, Operators, Expressions, Control Statements, Type Conversion And Casting, Concepts Of Classes, Objects, Constructors, Methods, Access Control, This Keyword, Garbage Collection, Overloading Methods And Constructors .�3) Introducingaccess Control, Static, Final, Nested And Inner Classes, Exploring String Class, Using Command-linear Arguments.Inheritance: Inheritance Concept, Types Of Inheritance, Member Access Rules, Use Of Super And Final. Polymorphism - Dynamic Binding, Method Overriding, Abstract Classes And Methods ��
OUTCOMES:
Achieve proficiency in object-oriented concepts and also learns to incorporate the same into the Java programming language
MODULE-I
MATRUSRI
ENGINEERING COLLEGE
PRINCIPLES OF OBJECT ORIENCTED PROGRAMMING
Encapsulation. Encapsulation is the mechanism of hiding of data implementation by restricting access to public methods.
Abstraction. Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract...
Inheritance. Inheritances expresses is-a and/or has-a relationship between two objects. Using Inheritance, In...
Polymorphism. It means one name many forms. It is further of two types static and dynamic.
Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding. It is closely related to inheritance. We can write a code that works on the superclass, and it will work with any subclass type as well.
MATRUSRI
ENGINEERING COLLEGE
MATRUSRI
ENGINEERING COLLEGE
BENEFITS OF OBJECT ORIENTED PROGRAMMING
It is possible that multiple instances of objects co-exist without any interference
JAVA BUZZWORDS
MATRUSRI
ENGINEERING COLLEGE
BYTECODE
Java bytecode is the instruction set for the Java Virtual Machine. It acts similar to an assembler which is an alias representation of a C++ code. As soon as a java program is compiled, java bytecode is generated. In more apt terms, java bytecode is the machine code in the form of a .class file. With the help of java bytecode we achieve platform independence in java.
APPLET AND APPLICATION PROGRAM USING SIMPLE JAVA PROGRAM
Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.
There are many advantages of applet. They are as follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.
DATA TYPES AND VARIABLES
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes Interfaces and Arrays
A variable is a name assigned to a value that is stored inside the system memory
MATRUSRI
ENGINEERING COLLEGE
ARRAYS
MATRUSRI
ENGINEERING COLLEGE
MATRUSRI
ENGINEERING COLLEGE
OPERATORS
Operator in Java is a symbol that is used to perform operations
There are many types of operators in Java
Unary Operator,
Arithmetic Operator,
Shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
MATRUSRI
ENGINEERING COLLEGE
EXPRESSIONS
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators.A Java expression consists of variables, operators, literals, and method calls. To know more about method calls,
For example:
Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;
Here, a + b - 3.4 is an expression.
MATRUSRI
ENGINEERING COLLEGE
CONTROL STATEMENTS
Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program.
Java provides three types of control flow statements.
1.Decision Making statements
if statements
switch statement
2.Loop statements
do while loop
while loop
for loop
for-each loop
3.Jump statements
break statement
continue statement
MATRUSRI
ENGINEERING COLLEGE
TYPE CONVERSION AND CASTING
In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer.
MATRUSRI
ENGINEERING COLLEGE
CONCEPTS OF CLASSES
In object-oriented programming, a class is a basic building block. It can be defined as template that describes the data and behavior associated with the class instantiation. Instantiating is a class is to create an object of that class that can be used to access the member variables and methods of the class.
In general, class declaration includes the following in the order as it appears:
MATRUSRI
ENGINEERING COLLEGE
ACCESS CONTROL
We can control the access level for class member variables and methods through access specifiers.
Java's access specifiers are public, private, protected and a default access level.
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of the method. Java provides four types of access specifier:
Public: The method is accessible by all classes when we use public specifier in our application.
Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.
Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible only from the same package only.
MATRUSRI
ENGINEERING COLLEGE
THIS KEYWORD
“this” is a reference variable that refers to the current object.
this can be used to refer current class instance variable.
this can be used to invoke current class method (implicitly)
this() can be used to invoke current class constructor.
this can be passed as an argument in the method call.
this can be passed as argument in the constructor call.
this can be used to return the current class instance from the method.
GARBAGE COLLECTION
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
MATRUSRI
ENGINEERING COLLEGE
MATRUSRI
ENGINEERING COLLEGE
OVERLOADING METHODS AND CONSTRUCTORS
Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time (or static) polymorphism.
// Java program to demonstrate working of method overloading in Java.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
MATRUSRI
ENGINEERING COLLEGE
Constructor overloading
The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
Program: Student.java
public class Student {
//instance variables of the class
int id;
String name;
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
MATRUSRI
ENGINEERING COLLEGE
STATIC
The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.
The static can be:
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
FINAL
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
variable
method
class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
MATRUSRI
ENGINEERING COLLEGE
MATRUSRI
ENGINEERING COLLEGE
NESTED AND INNER CLASSES
Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Nested classes are divided into two types
STRING CLASS IN JAVA
String is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created.
There are two ways to create string in Java:
String literal
String s = “matrusri”;
Using new keyword
String s = new String (“matrusri”);
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
MATRUSRI
ENGINEERING COLLEGE
COMMAND LINE ARGUMENTS
The java command-line argument is an argument i.e. passed at the time of running the java program.The arguments passed from the console can be received in the java program and it can be used as an input.So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Example CommandLineExample.java
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
} }
MATRUSRI
ENGINEERING COLLEGE
1.25
INHERITANCE
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.In java programming, multiple and hybrid inheritance is supported through interface only.
MATRUSRI
ENGINEERING COLLEGE
1.26
MEMBER ACCESS RULES
Superclass members can be inherited to subclass provided they are eligible by access modifiers. The behavior of access specifiers in the case of inheritance in java is as follows:
1. The private members of the superclass cannot be inherited to the subclass because the private members of superclass are not available to the subclass directly. They are only available in their own class.
2. The default members of the parent class can be inherited to the derived class within the same package.
3. The protected members of a parent class can be inherited to a derived class but the usage of protected members is limited within the package.
4. Public members can be inherited to all subclasses.
MATRUSRI
ENGINEERING COLLEGE
1.27
USE OF SUPER AND FINAL
SUPER
The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
Usage of Java super Keyword
MATRUSRI
ENGINEERING COLLEGE
1.28
FINAL
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only
MATRUSRI
ENGINEERING COLLEGE
1.29
POLYMORPHISM - DYNAMIC BINDING
“Dynamic” means “run time”, and “binding” means “association”. So the term dynamic binding indicates run time association of objects by java virtual machine.
MATRUSRI
ENGINEERING COLLEGE
1.30
METHOD OVERRIDING
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as Method Overriding.
Java Method Overriding is used for:
Rules for Java Method Overriding
There must be an IS-A relationship (inheritance).
MATRUSRI
ENGINEERING COLLEGE
1.31
ABSTRACT CLASSES AND METHODS
ABSTRACT CLASS
A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). It needs to be extended and its method implemented. It cannot be instantiated. It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.
ABSTRACT METHOD
Abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from)
MATRUSRI
ENGINEERING COLLEGE
END OF UNIT - I
32