Java Advance Notes
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.
Body…. }
Body…. }
Method Overloading : method overloading, multiple methods can have the same name with different
parameters . by declare no.of parameters or by declaring its datatype .
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();
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.
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.
Encapsulation : The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
To achieve this, you must:
Type Casting :
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.
Polymorphism :
Abstraction :
Interface :
Inner Classes :
iterator:
Exceptions :
thread :
Lambda :
Wrapper Classes :
Packages / API: