0% found this document useful (0 votes)
38 views23 pages

Sap Po-Pi

The document provides an overview of object-oriented programming concepts in Java including class structure, methods, objects, constructors, and this operator. It defines classes, demonstrates creating and calling methods, passing arguments to methods, creating and accessing objects, using constructors to initialize objects, and differentiates class variables from method parameters using this operator.

Uploaded by

Aahil Aaryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views23 pages

Sap Po-Pi

The document provides an overview of object-oriented programming concepts in Java including class structure, methods, objects, constructors, and this operator. It defines classes, demonstrates creating and calling methods, passing arguments to methods, creating and accessing objects, using constructors to initialize objects, and differentiates class variables from method parameters using this operator.

Uploaded by

Aahil Aaryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object oriented programming Lecture notes

In JAVA language.

JAVA BASIC STRUCTURE AND PRINTING STATEMENT

Class Demo OR public class test


{

static void main(String args[]) OR public static void main(String[] args)

System.out.println("Hallow world");
Main keywords
}
public - anywhere or the access modifiers.
Method static- access level
}
main() - main method
Class
void means - no return type
• there is only one main method.

PRINTING VAREABLE

Class Demo
{
static void main(String args[])
{
String name = "Raveen";
int age = 10;
System.out.println(name+""+age);
}
}
ADDING NUMBERS

public class test{


public static void main(String[] args)
{
int a=10,b=20,tot=0;
tot=a+b;
System.out.println(tot);
}
}
METHODS IN JAVA

CREATING AND CALLING A FUNTION (METHOD TYPE 1) Passing an arguments or data insert to the method
as parameters to the main function for the process
public class test{
in the main function. The reason is, the codes inside
public static void main(String[] args) -
of the main take to the array and passes to the main
{
function for the execution. “Args” means the array
printMessage();
name and [] use to denote the array
* When crating methods always use simple letters
Function created and avoid spaces use _ or Camal cases.

There are 2 types of methods


}
* void methods – to execute all of the lines
public static void printMessage() {
System.out.println("Hallow to funtions!"); * return type methods – to execute the returned
} value or selected line only
}
CREATING AND CALLING A FUNTION (METHOD TYPE 2)

public class test{


public static void main(String[] args)
{

int result=0; Using and calling of


result=getProduct(); function
System.out.println(result);
}
public static int getProduct() { Function created
return 10*2;
}
}
USING OF PARAMETERS IN METHODS Returning of
mathematical operation.
public class test{
public static void main(String[] args)
{
getData("Raveen", 17); Calling the function
inserting arguments
}
public static void getData(String name,int age) {
System.out.println(name+"\n"+age);
Arguments
}

}
CREATION OF OBJECTS

class Student Declaring parameter variables


Constructor of the
{ class (present or
String sname; absent)
int age; Creation of class for the object
double gpa;
Student s2=new Student();
public static void main(String args[])
{
Student s1=new Student(); Creating of the object Class name or
Object name or
Student s2=new Student(); custom data type
variable that we
s1.sname="sumith"; Or declaring object
assign
s1.age=22;
Inserting data to the
created object
s1.gpa=3.5;
System.out.println("The name - "+s1.sname+"\nThe age - "+s1.age+"\nThe GPA - "+s1.gpa);
s2.sname="Pavan";
s2.age=80;
s2.gpa=3.4;
System.out.println("The name - "+s2.sname+"\nThe age - "+s2.age+"\nThe GPA - "+s2.gpa);
}
}
OBJECTS AND FUNTIONS (FUNTIONS EXECUTABLE WITH ONLY OBJECT)

class Student
{
String sname;
int age;
double gpa;

public static void main(String args[])


{
Student s1=new Student();
s1.printMessage("Raveen",17,3.4);
System.out.println("The name - "+s1.sname+"\nThe age - "+s1.age+"\nThe GPA - "+s1.gpa);

}
public void printMessage(String aname,int aAge,double Agpa)
{
System.out.println("Hallow!!");
sname=aname;
age=aAge;
gpa=Agpa;

}
}
COMMON STRUCTURE

S1.printMessage();

Accessing to the function like accessing to a property of the object. – functions that works and calls
with only objects.

USE OF THE FUNTION

Student s1=new Student();


s1.printMessage("Raveen",17,3.4);
System.out.println("The name - "+s1.sname+"\nThe age - "+s1.age+"\nThe GPA - "+s1.gpa);

}
public void printMessage(String aname,int aAge,double Agpa)
{
System.out.println("Hallow!!");
sname=aname;
age=aAge;
gpa=Agpa;

}
USING OF 2 CLASSES

package lessons;

/**
* lesson
*/

public class raveen{


int lengh,width;
Class 1
void calcArea(int lengh,int width){
int area = lengh*width;
System.out.println(area);

}
}
/**
* area
*/

class Main{

public static void main(String[] args) { Class 2 that includes


raveen a1= new raveen(); the main function
raveen a2= new raveen ();
a1.calcArea(5, 10);
a2.calcArea(5, 20);

}
}
Constructors
public class constructors { Special functions that is capable of call itself
when an object is created relevant to the class
String sname;//instance vareable that the constructor is created.
int age;
double gpa;

constructors()

{ Created constructor
sname="Kaveen";

age=17;

gpa=3.3;

public static void main(String[] args)


Created object
{

constructors s1=new constructors(); Constructor that calls


class automatically within
System.out.println(s1.sname);
the object
}

} Using the object like normal

INSERING DATA USING CONSTRUCTORS


public class constructors2

int length;

int width;

int height;

Memory parameters to insert or


constructors2(int Alength,int Awidth,int Aheight) access the class properties with
using memory parameters.
{

length=Alength;

width=Awidth;

height=Aheight;

void getVolume()
{

int volume=length*height*width;

System.out.println(volume);

public static void main(String[] args) {

constructors2 c1=new constructors2(10, 20, 30); Inserting data according to


the order that the
c1.getVolume();
parameters are defined while
calling the constructor.
}

“THIS” OPERATOR IN CONSTRUCTORS

• Use – to separate the parameters with variables.


• This. pname – equals to the class variable not the parameter.
public class constructors2 Base class
{
int length;
int width; Equals
int height;

constructors2(int length,int width,int height)


{
Separate this.length=length;
variables this.width=width; Constructor
this.height=height;
}
void getVolume()
{
int volume=length*height*width;
System.out.println(volume); Object functions

}
public static void main(String[] args) {
constructors2 c1=new constructors2(10, 20, 30);
c1.getVolume(); Main method

}
AC
ACCESS MODIFIERS
Access modifiers information Non-access modifiers Information
Public Information is public Final Attributes that can’t overridden
Private Access within the class only Static Attributes in a class without creating object
Protected Ability to access only when inherited Abstract Use to hide the data
Default Visible within the package Transient Modifier used in serialization
Synchronized ensure only one thread can access a shared resource at a time
Volatile

1) Public
• A class, method or variable can be declared as public and it means that it is accessible from any
class.
• A public class, method, or variable can be accessed from any other class at any time.

public class acess_modifiers {


public String name="Raveen";
public String lname="Jayasanka"; Properties with
public String email="[email protected]"; public
public int age=20;
}
public class acess_modifiers2 {
public static void main(String[] args) {
acess_modifiers student = new acess_modifiers();
System.out.println(student.name);
System.out.println(student.age); Ability to access the
System.out.println(student.email); data in the “Public”
} class properties in
the same file
}

And also, we have the access like below,


Folder
Java files
Accessible to the class properties in “Mod1.java”
file to “Mod2.java” file

2) Private
• Allows a variable or method to only be accessed in the class in which it was created.
package AccessMod;
public class Mod1 {
private String name;
private int age;
Accessible is
limited within
this file only. But
we can access
the properties in
the file only
private double gpa;
public static void main(String[] args) {
Mod1 student1 = new Mod1();
student1.name="Jhonn";
student1.age=20;
student1.gpa=3.3;
System.out.println(student1.name);
}
}

But we can’t access the properties


within other files.

3) Protected
• Unlike “Private”, we have the access to the programs in the same folder. But this condition
available only for the inherited classes from one class to another class in different file.

When using “Private” modifier

We can’t access

When using “Public” modifier

Has got the access for the properties in the “Mod1.java”


file from “Mod2.java” file, Only there is an inheritance
from “Mod1.java” to “Mod2,java”

The protected keyword is an access modifier used for attributes, methods and constructors making
them accessible in the same package and subclasses.

private Protected
It can only be invoked from within the This can be invoked on any instance of the class,
implementation of a class or its subclass when that class is inherited to the accessing class.

USING OF “SUPPER” KEYWORD IN CLASSES

• The supper keyword refers to superclass (Parent) properties in another class.


• This is use to call superclass methods and properties of the parent class.
• The main benefit of this keyword - To avoid confusion between superclass and the subclasses
that have the methods in the same name.

Example 1 – for accessing the functions


package AccessMod;

public class Mod1 {


public void printing1()
{
System.out.println("Hallow to codes!!");
}
} Inherits from Mod1 to Mod2 to get access the
class Mod2 extends Mod1 { properties
public void printing2()
{
Reference to ” Mod2” class
System.out.println("Hallow again!!");
super.printing1(); Reference to “Mod1” class. So, use “super” keyword to get the access the
function in the “Mod1” class which inherits to “Mod2” class. (For calling)
}
public static void main(String[] args) {
Mod2 m1 = new Mod2();
m1.printing2();
}

All need to call the “printing2”


function in “mod2” Class.

Example 2 – to access the properties

public class day5 {


String colour="White";
}
class dog extends day5 {

String colour="black";
void printColour() Accessing the properties of
{ the day5 class and also It
System.out.println(colour); must be inherited the class
System.out.println(super.colour); that we going to access.
}
}
class TestSupper {
public static void main(String args[])

{
dog d=new dog();
d.printColour();
}
}
INHERITANCE

package AccessMod;
public class Mod1 {
public void printing1()
{ Inherits form “Mod1” to “Mod2”
System.out.println("Hallow to codes!!");
}
}
class Mod2 extends Mod1 {
public void printing2()
{
System.out.println("Hallow again!!");
super.printing1();
}
public static void main(String[] args) {
Mod2 m1 = new Mod2();
m1.printing2();
}
}
• Inheritance in java is the method to create a hierarchy between classes by inheriting from other
classes.
• The inheritance concept is two categories,
1) Subclass (child) – the class inherits from another class (parent class)
2) Supper class (parent) – the class being inherited from
• We use “extends” keyword to inherit classes.
• Also due to security reasons, we can use access modifiers to control the access between the
classes.
• Main benefit of using inheritance – For code reusability (reuse the attributes and methods of an
existing class when creating a new class).

THE “Final” KEYWORD

• If you make final - we can’t inherit the “final” class to another class (to atop the inheriting
process of a class)
• Main benefit – to enhance the security.

package AccessMod;
final class Mod1 {
public void printing1()
{
System.out.println("Hallow to codes!!");
}
}
class Mod2 extends Mod1 { We can’t access or inherit the “Mod1”
public void printing2() class. Because the “Mod1” class is final.
{
System.out.println("Hallow again!!");
super.printing1();
}
public static void main(String[] args) {
Mod2 m1 = new Mod2();
m1.printing2();
}
}
STATIC VAREABLES IN JAVA

Whenever a variable is declared as static, this means there is only one copy of it for the entire class, rather
than each instance having its own copy. A static method means it can be called without creating an
instance (object) of the class.

We don’t need to create an object according to the class, that the variable is created in the class to access
the variable or to modify the variable.

Example

package AccessMod;
class Mod1 {
public static int num =10;
}
class Mod2 extends Mod1 { We don’t need to create an object
public static void main(String[] args) { to access the variable. But we must
num++; have to create an object to access
System.out.println(num); the variable when we don’t use
“static”.
}
}

package AccessMod;
class Mod1 {
int num =10;
We can’t access the variable according
}
to this way.
class Mod2 extends Mod1 {
public static void main(String[] args) {
//num++;
System.out.println(num);
}
}
STATIC METHODS IN JAVA

A static method can be invoked without the need for creating an instance (object) of a class. static
method can access static data member and can change the value of it without creating an object.

Static methods are methods that belong to the class rather than to any specific instance of the class.
Static methods can be called directly on the class itself without needing to create an instance of the
class.

It is not necessary to create an object for the


class “student9” to call the object “change()”

SUMMERY OF ACCESS MODIFIERS


DEFAULT ACCESS MODIFIER

• In this modifier, we don’t use any special keyword.


• This is also called as, package private.
• This means that, all the members are visible within the same package.
• But this isn’t accessible from other packages.

Example

package AccessMod;
class Mod1 {
int num;
}
class Mod2 {
public static void main(String[] args) {
Mod1 m1=new Mod1();
m1.num=30;
}
}

PUSRPOSE OF INHERITENCE

Code Reusability: The code written in the Superclass is common to all subclasses. Child classes
can directly use the parent class code.

Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the
ways by which Java achieves Run Time Polymorphism.

Abstraction: The concept of abstract where we do not have to provide all details is achieved
through inheritance. Abstraction only shows the functionality to the user.

TYPES OF JAVA INHERITENCE


POLYMORPHISM IN JAVA

• Polymorphism allows us to perform a single action in different ways.


• In other words, polymorphism allows you to define one interface and have multiple
implementations.
• Polymorphism is two types,
• Compile-time polymorphism
• Runtime polymorphism

Compile time polymorphism / overloading

• It is also known as static polymorphism. This type of polymorphism is achieved by function


overloading or operator overloading.
• When there are multiple functions with the same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by changes in the number of
arguments or/and a change in the type of arguments.

Example

package AccessMod;
public class poly {
static int printing(int num1,int num2)
{ Same function name, same
int tot=num1+num2; //why we can enter same vareables variables and same memory
return tot; parameters but different
} parameter data types
static double printing(double num1,double num2)
{
double tot= num1+num2;
return tot;
}
public static void main(String[] args) {
int result=printing(10, 20);
double result2=printing(23.56, 22.45); Calling in the main method
System.out.println(result2);
System.out.println(result);
}
}

• With method overloading, multiple methods can have the same name with different
parameters.

Runtime compilation or polymorphism / Overriding functions


• It is also known as Dynamic Method Dispatch. It is a process in which a function call to the
overridden method is resolved at Runtime.
• This type of polymorphism is achieved by Method Overriding. Method overriding, on the other
hand, occurs when a derived class has a definition for one of the member functions of the
base class. That base function is said to be overridden.
• Functions with the same name but different tasks or processes. OR we can change the process
of a function in a class with a different class while inheriting the classes.
• Main condition – must inherit to a different class to change the functions with the same name.

Example

package AccessMod;
public class poly { Inherits from “poly” to “printing2” so we
void printing() can make different processes but with same
{ function name. but for the access, all we
System.out.println("Hallow world!!"); have to do is, create the separate objects
} but call those functions with the same
} name to obtain different results.
class printing2 extends poly {
void printing()
{
System.out.println("Hallow to codes");
}
public static void main(String[] args) {
printing2 p1=new printing2();
Must create an object with “printing2” class to obtain the different process
p1.printing();
poly p2 = new poly(); Must create an object with “poly” class to obtain the different process
p2.printing();
}
}
Same name
ADVANTAGES OF POLYMORPHISM IN JAVA

• Increases code reusability by allowing objects of different classes to be treated as objects of a


common class.
• Improves readability and maintainability of code by reducing the amount of code that needs to
be written and maintained.
• Supports dynamic binding, enabling the correct method to be called at runtime, based on the
actual class of the object.
• Enables objects to be treated as a single type, making it easier to write generic code that can
handle objects of different types.

DIFFERENCE BETWEEN OVERLOADING AND OVERIDING


Overloading Overriding
• Use to increase readability of the • Used to provide the specific
program. implementation of the method that is
• Performed within class already provided by its supper class.
• Parameter must be different • Occurs in 2 classes that have a
relationship.
• Parameter must be same.

DATA ABSTRACTION

• Data abstraction is the process of hiding certain details and showing only essential information
to the user.
• Abstraction can be achieved with either abstract classes or interfaces.

Only necessary information is shown

Abstraction

Complex information

• In Java, abstraction is achieved by interfaces and abstract classes. We can achieve 100%
abstraction using interfaces.
• Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited from another class).
• Abstract methods don’t have a body.
• In general, if a class contains at least one incomplete method (in programming
terms, one abstract method), the class itself is an abstract class. The term
abstract method tells you that the method has a declaration (or signature) but no
implementation.

COMMEN STRUCTURE

This class contains an incomplete


abstract class Bike {
method or not implemented so,
abstract void run(); this is an abstract class
}
• In other words, you can think of abstract members as virtual members without a default
implementation.
• A class that contains at least one abstract method must be marked as an abstract class.
• The subclass (the class that inherits the abstract class) must finish the incomplete task; that is, a
subclass needs to provide the complete method body for the abstract method, but if it fails to
provide that, the subclass itself will be marked as another abstract class.

EXAMPLE

package Abstraction;
abstract class Bike {
abstract void run();
}

Inherit the class from “bike” to “honda” to


class honda extends Bike{
complete the abstract method or to create the
void run(){ body of the “run” method
System.out.println("running safely..");
}

EXAMPLE 2 – formation 2

package AccessMod;

abstract class poly {


abstract void printing();
abstract void printing2();
public static void main(String[] args) {
poly p1 = new poly2();
p1.printing();
}
}

class poly2 extends poly {


void printing()
{
System.out.println("Hallow world!!");
}
void printing2()
{
System.out.println("Hallow to codes!!");
}
}
}
PRACTICAL USAGE

Main methods that contain


visible part to the user

Function parts that are


hided information from
the user.

Data and the processes that


hide from the user and for
the abstract bodies. The
part that the user cannot
see
INTERFACES IN JAVA

• The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not the method body.
Abstraction Interfaces

To access the abstracted part, we have to But in interfaces, hide the functions that I need to
create a sub class in each time when you are hide but allow access or calling the function in
going to access the abstract function implement part without creating a subclass for
each function that I need to access.

EXAMPLE
CONSTRUCTOR TYPES IN OOP

• There are 2 types of constructors in OOP they are,


• Default constructors - The constructor without the memory parameters.
• Parametric constructors - The constructor with memory parameters.
• Examples – For Default constructor
constructorName(){

}
• Example – For Parametric constructor
constructorName(datatype parameter1, datatype parameter2……..){

USING OF DEFAULT AND PARAMETRIC CONSTRUCTORS AT THE SAME TIME

• Main use – to use 2 different constructors of the same class to create 2 different processes.

EXAMPLE
class student2 {
String name,DOB,address;
int age;
student2(String name,String DOB,String address,int age){
this.name=name;
this.DOB=DOB; Constructor 1 with parameters to
this.address=address; enter data.
this.age=age;
}
student2(){ Constructor type 2 without memory
System.out.println("No data added!!"); parameters
}
}
public class student {
public static void main(String[] args) {
student2 s1=new student2("Jhonn", "2005-02-15", "California", 20);
2 different outputs
student2 s2=new student2();
}
}

OVERLOADING OF CONSTRCUTORS

• In this case, there are different parameters of the 2 different constructors.

EXAMPLE
class student2 {
String name,DOB,address;
int age,phone;
student2(String name,String DOB,String address,int age){
this.name=name;
this.DOB=DOB;
this.address=address; Different parameters
this.age=age;
}
student2(int phone){
this.phone=phone;
}
}
public class student {
public static void main(String[] args) {
student2 s1=new student2("Jhonn", "2005-02-15", "California", 20);
student2 s2=new student2(778733391);
}
}

ENCAPSULATION IN OOP

In object-oriented programming, you do not allow your data to flow freely inside the
system. Instead, you wrap the data and functions into a single unit (i.e., in a class). The
purpose of encapsulation is at least one of the following:

• inserting restrictions in place so that the components of an object


cannot be accessed directly
• Binding the data with methods that will act on that data (i.e., forming a capsule).
GETTERS AND SETTERS
• Main use – to access the “private” attributes.
• Main benefits of using getters and setters
• You can provide controlled access to your data.
• You can make your class variable either read-only or write-only. When you provide the getter
method only, you can only get the value of the private variable, so it becomes read-only.
Similarly, when you provide only the setter method, you make the variable write-only.

EXAMPLE

public class lesson1 {


private String name;
public String getage()
{
Getter – to return the value only at this stage.
return name;
}
public void setAge(String name)
{ Setter – to access the data or
this.name=name; inserting data.
}
}
class section {
public static void main(String[] args) {
lesson1 l1=new lesson1();
l1.setAge("Sanuja");
System.out.println(l1.getage());
}
}

PACKAGES IN JAVA

• A set of classes and interfaces grouped together are known as Packages.


• Every class is a part of a certain package.
• When we want to use an existing class, we have to add that package into the java program.
• Benefits,
• The packages organize the group of classes into a single API unit.
• Ability to control naming conflicts.
• Ability to Reuse the packages.
• There are 2 types of packages They are, - User defined Packages
In built packages

EXAMPLE
Name of the package(folder)
package tools;

public class tool1 {


public void process1(){
System.out.println("Test passed!!");
Processes or classes that are
process3();
}
included inside the package.
public static void process3(){
System.out.println("Test3 passed!!");
}
}

IMPROTING PACKAGES

import tools.tool1;
public class test{
public static void main(String[] args) {
tool1 t1=new tool1();
t1.process1();

}
}

You might also like