INTRO TO OOP - 2nd2023 - 24
INTRO TO OOP - 2nd2023 - 24
Self-paced activity:
Java OOPs Concepts
a. https://www.javatpoint.com/java-oops-concepts
b. https://youtu.be/pTB0EiLXUC8
c. https://youtu.be/m_MQYyJpIjg
➢ Objects from different classes do not share the same definition of attributes
or methods.
Example:
Class vs. Object
A Class consists of :
1. A unique name (starts with an uppercase letter)
2. A list of attributes (int, double, Boolean, String, etc)
- attributes are the variables you define when creating a class
3. A list of methods
This is shown in a simple box structure:
Instance variable
➢ It is a variable defined within a class, each instance of the class (that is, each object of
the class) contains its own copy of these variables.
Multiple Objects
➢ If you create multiple objects of one class, you can change the attribute values in
one object, without affecting the attribute values in the other.
Constructors
➢ You use a constructor to give initial values to the instance variables defined by
the class.
➢ All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables
to zero. However, once you define your own constructor, the default constructor
is no longer used.
2. Parameterized constructor
- A constructor which has a specific number of parameters
- used to provide different values to distinct objects.
Code: OOPTriangle
Encapsulation
➢ Encapsulation is one of the four fundamental OOP concepts.
➢ a process of wrapping code and data together into a single unit
➢ the variables of a class will be hidden from other classes, and can be accessed only
through the methods of their current class (also known as data hiding)
Benefits of Encapsulation
➢ The fields of a class can be made read-only or write-only.
➢ A class can have total control over what is stored in its fields.
Code: OOPTriangle
Java Package
➢ A group of similar types of classes, interfaces and sub-packages.
3. User-defined package.
1. package statements
A package in Java is a mechanism to encapsulate a group of classes, sub-packages,
and interfaces.
(Imports and packages : https://youtu.be/ipMdsje9J6s)
2. import statements
The import statement is used to import a package, class, or interface.
3. class definitions
Important Points to keep in mind while working with Java Source File
1. A Java program can contain any number of classes, and at most, one of them can
be declared as a public class.
2. The name of the Java source file can be anything provided that no class is declared as
public.
Code: MultiClass
class Student
{
int ID;
String name;
static String college ="ITS";
Student(int r, String n)
{
ID = r;
name = n;
}
void display ()
{
System.out.println(ID+" "+name+" "+college);}
}
}
}
What is the output of this program?
class Counter
{
int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Sample Program:
class Calculate
{
static int cube(int x)
{
return x*x*x;
}
public static void main(String args[])
{
int result=cube(5);
System.out.println(result);
System.out.println(Calculate.sum(2,3));
}
static int sum(int x, int y)
{ return x+y; }
}