Sap Po-Pi
Sap Po-Pi
In JAVA language.
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
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.
}
CREATION OF OBJECTS
class Student
{
String sname;
int age;
double 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.
}
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
*/
}
}
/**
* area
*/
class Main{
}
}
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;
int length;
int width;
int height;
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);
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.
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);
}
}
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.
We can’t access
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.
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).
• 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.
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.
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.
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
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.
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
EXAMPLE
package Abstraction;
abstract class Bike {
abstract void run();
}
EXAMPLE 2 – formation 2
package AccessMod;
• 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
}
• Example – For Parametric constructor
constructorName(datatype parameter1, datatype parameter2……..){
• 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
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:
EXAMPLE
PACKAGES IN JAVA
EXAMPLE
Name of the package(folder)
package tools;
IMPROTING PACKAGES
import tools.tool1;
public class test{
public static void main(String[] args) {
tool1 t1=new tool1();
t1.process1();
}
}