0% found this document useful (0 votes)
14 views3 pages

Java Advance Notes

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views3 pages

Java Advance Notes

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Java Advance Notes

Method / Function :

A method is a block of code which only runs when it is called. you can pass data, known as
parameters, into a method. 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.

Syntax : static void methodname (){

Body…. }

Method call : inside main method declare methodename();

For parameters and arguments :

Syntax : static void methodname( datatype var1 , datatype var2 …..){

Body…. }

Inside main method : methodname( value1 , value2);

Method Overloading : method overloading, multiple methods can have the same name with different
parameters . by declare no.of parameters or by declaring its datatype .

Recursion : Recursion is the technique of making a function call itself .

Syntax : public class Main {


public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}

Method Overriden : method overriding occurs when a subclass (child class) has the same method as the
parent class .
Syntax : class Parent {–
void show() {
System.out.println("Parent"); }
}
class Child extends Parent {

void show()
{
System.out.println("Child");
}

}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();

Parent obj2 = new Child();


obj2.show();
}
}

OOPS : Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to
maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter development
time

Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should
extract out the codes that are common for the application, and place them at a single place and reuse them
instead of repeating it.

Four pillars of OOPs : encapsulation, abstraction, inheritance, and polymorphism.

Class & Objects : a class describes the contents of the objects that belong to it: it describes an aggregate of data
fields (called instance variables), and defines the operations (called methods). object: an object is an element (or
instance) of a class; objects have the behaviors of their class.
Syntax : public class name { …… }
To access class variable and methods , inside main method create its object : classname objectname = new
classname();
You can create multiple object of a class . variable inside class known as attribute . to access attribute , inside main
method : objectname.variablename ;

Constructor : A constructor in Java is a special method that is used to initialize objects. The constructor is called when
an object of a class is created.

Modifiers : it gives accessibility to methods and class .


 Public : accessible by any other class and methods .
 Private : accessible within cdeclared class .
 Protected : accessible only by inherited class
 Final : can’t be overriden
 Abstract : class cannot be used to create objects (To access an abstract class, it must be inherited from
another class )
 Static : it indicates attribute and methods belong to the class .

Encapsulation : The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
To achieve this, you must:

 declare class variables/attributes as private


 provide public get and set methods to access and update the value of a private variable . get
meaning read only , set meaning write only .

example : public class Encap {


private int a;
public String getVar() {return a;}
public void setVar(int b) {this.a = b;}}
public class Main {
public static void main(String[] args) {
Encap Obj = new Encap();
Obj.setVar(10);
System.out.println(Obj.getVar());
}
}

Type Casting :

 Widening Casting (automatically) - converting a smaller type to a larger type size


byte -> short -> char -> int -> long -> float -> double

 Narrowing Casting (manually) - converting a larger type to a smaller size type


double -> float -> long -> int -> char -> short -> byte

Ex : int a = 10 ;
Float a1 = a ; //10.0

Ex : float q = 10.0;
Int a = q; // 10

Inheritance : Inheritance means creating new classes based on existing ones. A class
that inherits from another class can reuse the methods and fields of that class.

Syntax : class subClassName extends superClassName{ ……. }

Types of inheritance : Types of Inheritance in Java: Single, Multiple, Multilevel &


Hybrid

Polymorphism :
Abstraction :
Interface :
Inner Classes :
iterator:
Exceptions :
thread :
Lambda :
Wrapper Classes :
Packages / API:

You might also like