Module-1
Module-1
By:
Dr. Nagendra Panini Challa
Assistant Professor, Senior Grade 2
SCOPE, VIT-AP University, India
Basics of OOP this Keyword
Features of OOP Encapsulation
Introduction to Java Inheritance: Inheritance
Objects and Classes in Java Hierarchies
Defining Classes Super and Subclasses
The main aim of OOP is to bind together the data and the functions that operate on
them so that no other part of the code can access this data except that function.
By defining sets of classes that represent and encapsulate objects in a program, the
classes can be organized into modules, improving the structure and organization of
software programs.
Thus, developers often use OOP as a tool when they need to create complex
programs since they are easier to reason in terms of classes and their relationships.
It is used to write,
so writing is its
behavior.
P can be
identifier
Defined by an instance
Collection of objects is called class.
(Object)
It is a logical entity.
An object is an instance of a class. Name, Roll number,
Branch (Properties of
We can create many instances of a class.
the object)
A Java class uses variables to define data
fields and methods to define actions.
Read(), Write() and
A class can also be defined as a blueprint Play() (Tasks
from which you can create an individual Performed)
object.
}
Object Oriented Programming (OOP), SCOPE, VIT-AP University, India 21-02-2023 12
It is a process of hiding implementation details and exposes only the functionality
to the user.
In abstraction, we deal with ideas and not events.
This means the user will only know “what it does” rather than “how it does”.
Real-Life Example-
A driver will focus on the car functionality (Start or Stop, Accelerate or Break), he
or she does not bother about how the Accelerate or brake mechanism works
internally. And this is how the abstraction works.
Real-life Example:
A delivery person delivers items to the user. If it’s a postman he will deliver the
letters. If it’s a food delivery boy he will deliver the foods to the user. Like this
polymorphism implemented different ways for the delivery function.
Real-life Example:
The planet Earth and Mars inherits the super class Solar System and Solar system
inherits the Milky Way Galaxy. So Milky Way Galaxy is the top super class for Class
Solar System, Earth and Mars.
The Java Runtime Environment (JRE) is a set of software tools which are used for
developing Java applications.
Single-line Comment It starts with a pair of forwarding slash (//) // Hello VIT-AP
Multi-line Comment It starts with a /* and ends with */. We write between these two
symbols.
/∗It is an example of multiline comment∗/
Documentation Comment It starts with the delimiter (/**) and ends with */.
/∗∗ I t i s an example of documentation comment∗/
In this section, we declare the package name in which the class is placed.
Note that there can be only one package statement in a Java program. It must be
defined before any class and interface declaration.
For Example
package scope; //where scope is the package name
package VIT-AP.scope;
//where VIT-AP is the root directory and scope is the subdirectory
If we want to use any class of a particular package, we need to import that class.
The import statement represents the class stored in the other package.
It is written before the class declaration and after the package statement.
For example:
import java.util.Scanner; //it imports the Scanner class only
import java.util.*; //it imports all the class of the java.util package
An interface can also be used with other interfaces by using the extends keyword.
For example:
interface car
{
void start();
void stop();
}
Every Java program has at least one class that contains the main() method.
For example:
class Student //class definition
{
public static void main(String[] args)
//statements
}
For example:
class Student //class definition
{
String sname; //variable
int id;
double percentage;
}
Because the execution of all Java programs starts from the main() method.
Inside the main method, we create objects and call the methods.
For example:
public class Student //class definition
{
public static void main(String args[])
{
//statements
}
}
Without the main() method, JVM will not execute the program.
For example:
public class Test {
public void main(String [] args){
System.out.println(”Hello World”);
}}
For example:
public class Test {
public static void main(String [] args){
return 0;
}}
String args[]: The main() method also accepts some data from the user. It accepts
a group of strings, which is called a string array. It is used to hold the command line
arguments in the form of string values.
String - stores text, such as ”Hello”. String values are surrounded by double quotes
int- stores integers (whole numbers), without decimals, such as 123 or -123
float- stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as ’a’ or ’B’. Char values are surrounded by
single quotes
boolean - stores values with two states: true or false
To create a variable that should store text, look at the following example:
String name = ”John ”; System.out. println(name);
To create a variable that should store a number, look at the following example:
int myNum = 15;
System.out. println(myNum );
Note that if you assign a new value to an existing variable, it will overwrite
the previous value:
int myNum = 15;
myNum = 20; // myNum is now 20
System . out . println(myNum);
Instance variable:
A variable declared inside the class but outside the body of the method, is called
instance variable.
Example
int x = 10;
x += 5;
For Example
To create an object of Main, specify the class name, followed by the object name, and
use the keyword new
Example
public class VIT {
int x=5;
public static void main(String [] args)
{
VIT myObj = new VIT ( ) ; Output: 5
System.out. println(myObj.x);
}}
5
5
Object Oriented Programming (OOP), SCOPE, VIT-AP University, India 21-02-2023 65
You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all the attributes and
methods, while the other class holds the main() method (code to be executed)).
Main.java
public class Main {
int x=5;
}
Second.java
class Second {
public static void main(String [] args) {
Main myObj = new Main ( ) ;
System.out. println(myObj.x);
}
Output: 5
Example
public class Main {
final int x = 10;
public static void main(String [] args) {
Main myObj = new Main ( ) ;
myObj.x = 25; // will generate an error : cannot be assigned
System.out. println(myObj.x);
}}
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it many times.
In the following example, myMethod(); is used to print a text (the action), when it is
called:
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
Output:
I just got executed
}}
Constructor Parameters
public class Main {
int x;
public Main( int y) { //parameterised constructors
x=y;
}
public static void main(String [] args) { Output:
Main myObj = new Main (5);
System.out. println(myObj.x); 5
}}
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.
Object Oriented Programming (OOP), SCOPE, VIT-AP University, India 21-02-2023 100
Object Oriented Programming (OOP), SCOPE, VIT-AP University, India 21-02-2023 101
Object Oriented Programming (OOP), SCOPE, VIT-AP University, India 21-02-2023 102