OOPs
---------
---->Object Oriented Programming System.
1)object
2)class
3)Inheritance
4)Polymorphism
5)DataAbstraction
6)Encapsulation
I.Object
------------------
---->It is a real world Entity.
---->Instance of the class.
Properties
----------------------
1)state
2)behaviour
EX:
-----
car,mobile,pen,chair,....
car
state---->color,model,price,...
behaviour--->start,stop,...
2)class
-------------
---->collection of an Object.
syntax:
---------------
class classname
{
//variables
//methods
}
state+behaviour(object)= class
EX:
-----
class company
{
name,city,pincode; --->state
open()
close()
}
3)Inheritance
--------------------
Inheritance is a mechanism where a new is derived from an existing class.
4)polymorphism
-------------------------------
--->if single task is process by different ways.
5)DataAbstraction
----------------------------------
---->it hiding internal details and Show only Functionality.
6)Encapsulation
-------------------------------
---->it is a process of wrapping data into single Unit.
I.class and Object
---------------------------------
package Mypack1;
class Besant
{
public static void Besant_Technologies()//static method
{
System.out.println("Welcome to Besant Class");
}
}
public class Myclass3
{
public static void main(String[] args)
{
//A method as a static call from
//classname.methodname()
Besant.Besant_Technologies();
System.out.println("This is Main method");
}
-------------------------------------------------------
EX:
-----
package Mypack1;
class Besant
{
public void Besant_Technologies()//non-static method
{
System.out.println("Welcome to Besant Class");
}
}
public class Myclass3
{
public static void main(String[] args)
{
//A method as a non-static call from
//classname objectname=new classname()
Besant b1=new Besant();
b1.Besant_Technologies();
System.out.println("This is Main method");
}
}
---------------------------------------------------------------------
Inheritance
-----------------------
Inheritance is a mechanism where a new is derived from an existing class.
syntax:
----------------
class Parentname
{
//statements
}
class childclass extends Parentclass
{
//statements
}
types:
-------------
1)single Inheritance
2)multilevel Inheritance
3)Hierarchical Inheritance
4)multiple Inheritance(Not supported in java)
5)Hybrid Inheritance(Not supported in java)
1)single Inheritance
---------------------------------
A (Parent)
|
B (Child)
EX:
-----
package Mypack1;
class Bike
{
public void Bike_method()
{
System.out.println("The Bike is moving..");
}
}
class Car
{
public void Car_Method()
{
System.out.println("The Car is Running...");
}
}
public class Myclass3
{
public static void main(String[] args)
{
Bike b1=new Bike();
b1.Bike_method();
Car c1=new Car();
c1.Car_Method();
}
EX:
-----
package Mypack1;
class Bike
{
public void Bike_method()
{
System.out.println("The Bike is moving..");
}
}
class Car extends Bike
{
public void Car_Method()
{
System.out.println("The Car is Running...");
}
}
public class Myclass3
{
public static void main(String[] args)
{
//Bike b1=new Bike();
//b1.Bike_method();
Car c1=new Car();
c1.Car_Method();
c1.Bike_method();
}
}
------------------------------------------------------------------------------
Multilevel Inheritance
-----------------------------------------
A (Grand Parent)
|
B (Parent)
|
C (Child)
|
D (Grand Child)
EX:
-----
package Mypack1;
class Book
{
public void Book_details(String Bname)
{
System.out.println("Book Name is "+Bname);
}
}
class Author extends Book
{
public void Author_Details(String Aname)
{
System.out.println("Author Name is "+Aname);
}
}
class Price extends Author
{
public void Price_details(int money)
{
System.out.println("Book Price is "+money);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Price p1=new Price();
p1.Book_details("Java");
p1.Author_Details("John");
p1.Price_details(500);
}
----------------------------------------------------------------------
package Mypack1;
import java.util.Scanner;
class Book
{
public void Book_details(String Bname)
{
System.out.println("Book Name is "+Bname);
}
}
class Author extends Book
{
public void Author_Details(String Aname)
{
System.out.println("Author Name is "+Aname);
}
}
class Price extends Author
{
public void Price_details(int money)
{
System.out.println("Book Price is "+money);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Price p1=new Price();
String a,b;
int c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Details");
a=sc.next();
b=sc.next();
c=sc.nextInt();
p1.Book_details(a);
p1.Author_Details(b);
p1.Price_details(c);
}
------------------------------------------------------------------------------
Hierarchical Inheritance
--------------------------------------------
A
|
----------------------------
| | |
B C D
EX:
-----
package Mypack1;
class Calculation
{
public void calculate()
{
System.out.println("This is Calculation class");
}
}
class Addition extends Calculation
{
public void add(int x,int y)
{
System.out.println("Adding two value is"+(x+y));
}
}
class Subtraction extends Calculation
{
public void sub(int i,int j)
{
System.out.println("Subtraction is "+(i-j));
}
}
class Multiplication extends Calculation
{
public void mul(int s1,int s2)
{
System.out.println("Multiplication is "+(s1*s2));
}
}
public class Myclass3
{
public static void main(String[] args)
{
Multiplication m1=new Multiplication();
m1.calculate();
m1.mul(8, 6);
}
}
-----------------------------------------------------------
Multiple Inheritance
-------------------------------------
A B
| |
----------
|
C
Hybrid Inheritance
--------------------------------
A
|
------------------
| |
B C
| |
-----------------------
|
D
Hierarchical and multiple Inheritance
-----------------------------------------------------------------------------------
-------
Uml Diagrams
-------------------------
Polymorphism
---------------------------
Poly--->many
morphism--->form
"15"+"3"=153
15+3=18
types:
------------
1)compile time Polymorphism(or)method overloading
2)Run time Polymorphism(or)method overriding
1)compile time Polymorphism(or)method overloading
-----------------------------------------------------------------------------------
---------
--->Same method name but different Parameter.
EX:-(same datatype)
--------------------------------------
package Mypack1;
class Compiletime
{
public void test()//no Argu
{
System.out.println("Besant Technolgies");
}
public void test(int a)//one arg
{
System.out.println("The Result is "+a);
}
public void test(int y,int z)//two
{
System.out.println("The Result is "+(y*z));
}
public void test(int a1,int b1,int c1)//three arg
{
System.out.println("The Result is "+(a1+b1*c1));
}
}
public class Myclass3
{
public static void main(String[] args)
{
Compiletime c1=new Compiletime();
c1.test();
c1.test(23);
c1.test(2,3);
c1.test(1,2,3);
}
}
-------------------------------------------------
package Mypack1;
class Compiletime
{
public void test(String i,String j)//two
{
System.out.println("The Result is "+(i+j));
}
public void test(int a,float b)//two arg
{
System.out.println("The Result is "+a);
}
public void test(int y,int z)//two
{
System.out.println("The Result is "+(y*z));
}
public void test(int a1,int b1,int c1)//three arg
{
System.out.println("The Result is "+(a1+b1*c1));
}
}
public class Myclass3
{
public static void main(String[] args)
{
Compiletime c1=new Compiletime();
c1.test("Besant","Technologies");
c1.test(23,89);
c1.test(2,3);
c1.test(1,2,3);
}
}
---------------------------------------------------------------------------
run time polymorphism
------------------------------------------
--->same method name and same arguments
EX:
-----
package Mypack1;
class Animal
{
public void eat()
{
System.out.println("The Animal is Eating..");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("The Dog is Eating..");
}
}
public class Myclass3
{
public static void main(String[] args)
{
Dog d1=new Dog();
d1.eat();
d1.eat();
---------------------------------------------
package Mypack1;
class Bank
{
public void interest()
{
System.out.println("No Interest");
}
}
class Axis extends Bank
{
public void interest()
{
System.out.println("5% interest");
}
}
class SBI extends Bank
{
public void interest()
{
System.out.println("10% interest");
}
}
class ICICI extends Bank
{
public void interest()
{
System.out.println("3% Interest");
}
}
public class Myclass3
{
public static void main(String[] args)
{
SBI s1=new SBI();
s1.interest();
}
}
-----------------------------------------------------------------------------------
--------
DataAbstraction
------------------------------
--->It hiding internal details and show the Functionality.
types:
-----------
1)Data Abstraction
2)Interface
1)Data Abstraction
---------------------------------
1)abstract class
2)abstract method
I.abstract class
--------------------------
syntax:
--------------
abstract class classname
{
//abstract method
//non-abstract method
}
EX:
-----
package Mypack1;
abstract class Shape
{
public void print()//non-abstract method
{
System.out.println("This is Shape class");
}
abstract public void area();
}
class Circle extends Shape
{
public void area()
{
int r=7;
System.out.println("Area of Circle is "+(3.14f*r*r));
}
}
public class Myclass3
{
public static void main(String[] args)
{
Circle c1=new Circle();
c1.print();
c1.area();
}
}
-----------------------------------------------------------
Ex:
------
package Mypack1;
abstract class Mobile
{
public void voice_call()
{
System.out.println("You can make a voice call Process");
}
abstract public void camera();
abstract public void display();
}
class Samsung extends Mobile
{
public void camera()
{
System.out.println("12 Mexa Pixel");
}
public void display()
{
System.out.println("5.5 inch");
}
}
class Oppo extends Mobile
{
public void camera()
{
System.out.println("10 Mexa Pixel");
}
public void display()
{
System.out.println("5 inch");
}
public void fingerprint()
{
System.out.println("Fast sensor Finger print");
}
}
public class Myclass3
{
public static void main(String[] args)
{
Samsung s24=new Samsung();
System.out.println("Samsung product Details");
s24.voice_call();
s24.display();
s24.camera();
System.out.println("-----------------------");
Oppo reno11=new Oppo();
System.out.println("OPPO Product Details..");
reno11.voice_call();
reno11.display();
reno11.camera();
reno11.fingerprint();
}
-----------------------------------------------------------------------------------
--
Interface
-------------------
---->Interface is a blue print of class
interface interfacename
{
//statements
}
class extends class
interface extends interface
class implements interface
Ex:
-----
package Mypack1;
interface A
{
public void read();
interface B extends A
{
public void write();
}
class Demo implements B
{
public void read()
{
System.out.println("I am Reading a Book");
}
public void write()
{
System.out.println("I m Writing a story");
}
}
public class Myclass3
{
public static void main(String[] args)
{
Demo d1=new Demo();
d1.read();
d1.write();
}
}
-----------------------------------------------------------------------------------
----
Constructor
----------------------
---->Constructor is a special type of method that is used to object initialization
rules:
------------
1)class name same as method name
2)no return type
syntax:
--------------
class classname
{
classname()
{
//statements
}
1)Default Constructor
2)Parameterized Constructor
3)copy Construtor
1)Default Constructor
----------------------------------------
A Constructor have no arguments is called as a default Constructor
EX:
-----
package Mypack1;
class Bird
{
Bird()//default costructor
{
System.out.println("The Bird fly in the Sky..");
}
public void mymethod()//method
{
System.out.println("I am Listening...");
}
}
public class Myclass3
{
public static void main(String[] args)
{
Bird b1=new Bird();
b1.mymethod();
-----------------------------------------------------------------------------------
--------------
2)Parameterized Constructor
--------------------------------------------------
A Constructor have ANY arguments is called as a default Constructor
syntax:
----------------
class classname
{
classname(list of arg)
{
//statements
}
}
EX:
-----
package Mypack1;
class Employee
{
Employee(int Eid,String Ename,String company)
{
System.out.println(Eid+" "+Ename+" "+company);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Employee e1=new Employee(101,"Employee1","Wipro");
-------------------------------
EX:
-----
package Mypack1;
class Employee
{
Employee(int Eid,String Ename,String company)
{
System.out.println(Eid+" "+Ename+" "+company);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Employee e1=new Employee(101,"Employee1","Wipro");
Employee e2=new Employee(102,"Employee2","CTS");
}
}
-----------------------------------------------------------------------------------
---------
EX:
-----
package Mypack1;
class Employee
{
int id;
String en,c;
Employee(int Eid,String Ename,String company)
{
id=Eid;//id=101
en=Ename;//en=Employee1
c=company;//c=wipro
}
public void Employee_details()
{
System.out.println(id+" "+en+" "+c);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Employee e1=new Employee(101,"Employee1","Wipro");
e1.Employee_details();
}
}
-----------------------------------------------------------------------------------
---
copy constructor
-----------------------------
package Mypack1;
class Employee
{
int id;//Instance Variable
String en,c;
Employee(int Eid,String Ename,String company)
{
id=Eid;//id=101
en=Ename;//en=Employee1
c=company;//c=wipro
}
Employee(Employee obj)
{
id=obj.id;
en=obj.en;
c=obj.c;
}
public void Employee_details()
{
System.out.println(id+" "+en+" "+c);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Employee e1=new Employee(101,"Employee1","Wipro");
e1.Employee_details();
Employee e2=new Employee(e1);
e2.Employee_details();
}
----------------------------------------------------------------------------
Constructor overloding
------------------------------------------
package Mypack1;
class Employee
{
int id;//Instance Variable
String en,c;
Employee(int Eid,String Ename,String company)
{
id=Eid;//id=101
en=Ename;//en=Employee1
c=company;//c=wipro
}
Employee(int Eid,String name)
{
id=Eid;
en=name;
}
public void Employee_details()
{
System.out.println(id+" "+en+" "+c);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Employee e1=new Employee(101,"Employee1","Wipro");
e1.Employee_details();
Employee e2=new Employee(102,"Employee2");
e2.Employee_details();
}
----------------------------------------
Keywords
---------------------
---->Java used 50 keywords
--->predefined
--->all keywords are written in lowercase letter.
1)static
2)this
3)super
4)final
I.static
-------------
--->static variable
--->static method
--->static block
I.static variable
-----------------------------
package Mypack1;
class Student
{
int id;
String n,d;
static String college="PSG";
Student(int sid,String name,String dept)
{
id=sid;
n=name;
d=dept;
}
public void Student_details()
{
System.out.println(id+" "+n+" "+d+" "+college);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Student s1=new Student(1,"Student1","CSE");
s1.Student_details();
}
----------------------------------------------------------------------------------
static method
--------------------------
package Mypack1;
class Student
{
int id;
String n,d;
static String college="PSG";
public static void change()
{
college="SRM";
}
Student(int sid,String name,String dept)
{
id=sid;
n=name;
d=dept;
}
public void Student_details()
{
System.out.println(id+" "+n+" "+d+" "+college);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Student.change();
Student s1=new Student(1,"Student1","CSE");
s1.Student_details();
}
--------------------------------------------------------------------------------
static block
---------------------
package Mypack1;
public class Myclass3
{
public static void main(String[] args)
{
System.out.println("This is main block");
static
{
System.out.println("This is Static Block-1");
}
static
{
System.out.println("This is Static Block-2");
}
}
-----------------------------------------------------------------------------------
---
this keyword
------------------------
---->it represent the current object in the class.
types
------------
1).(dot)
2)off ()
I.this (.)
--------------
package Mypack1;
class Product
{
int id,price;
String name;
Product(int id,String name,int price)
{
this.id=id;
this.name=name;
this.price=price;
}
public void Product_details()
{
System.out.println(id+" "+name+" "+price);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Product p1=new Product(555,"Rice",98);
p1.Product_details();
-------------------------------------------------------
this Off()
------------------
package Mypack1;
class A
{
public void one()
{
System.out.println("This is One");
}
public void two()
{
System.out.println("This is Two");
}
public void three()
{
System.out.println("This is Three");
one();//this.one();
this.two();
}
}
public class Myclass3
{
public static void main(String[] args)
{
A obj=new A();
//obj.one();
//obj.two();
obj.three();
}
----------------------------------------------------------------------------
super
----------
---->it represent the Parent object in the class.
types:
--------------
1)variable
2)method
3)constructor
I.variable
-----------------
package Mypack1;
class Sample
{
String color="Red";
}
class Besant extends Sample
{
String color="Yellow";
public void print_details()
{
System.out.println("Color name is "+color);
System.out.println("Color name is "+super.color);//Red
}
}
public class Myclass3
{
public static void main(String[] args)
{
Besant b1=new Besant();
b1.print_details();
}
-----------------------------------------------------------------
method
-------------------
package Mypack1;
class Sample
{
String color="Red";
public void display1()
{
System.out.println("This is "+color+"color");
}
class Besant extends Sample
{
public void display1()
{
System.out.println("Display1");
}
public void display2()
{
System.out.println("display2");
}
public void print_details()
{
System.out.println("This is Besant Class");
display1();
display2();
super.display1();//parent class method represented
}
}
public class Myclass3
{
public static void main(String[] args)
{
Besant b1=new Besant();
b1.print_details();
}
}
---------------------------------------------------------------
constructor
-------------------
package Mypack1;
class Employee
{
Employee()
{
System.out.println("This is Employee class");
}
}
class HR extends Employee
{
HR()
{
super();
System.out.println("This is HR class");
//super();--->Error
}
}
public class Myclass3
{
public static void main(String[] args)
{
HR obj=new HR();
}
}
----------------------------------------------------------------------
final
----------
1)variable
2)class
3)method
final variable
--------------------------
---->A variable as a final can't be change it.
Ex:
-----
package Mypack1;
class Besant
{
int min=0;
final int normal=50;
int max;
Besant(int m)
{
max=m;
min=10;
}
public void print_data()
{
System.out.println(min+" "+normal+" "+max);
}
}
public class Myclass3
{
public static void main(String[] args)
{
Besant b1=new Besant(100);
b1.print_data();
}
-----------------------------------------------------------------------------
final method
------------------------
package Mypack1;
class Besant1
{
public void A()
{
System.out.println("A method");
}
final public void B()
{
System.out.println("This is B method");
}
}
class Besant2 extends Besant1
{
public void A()
{
System.out.println("This is A method");
}
/* public void B()
{
System.out.println("B method");
}*/
}
public class Myclass3
{
public static void main(String[] args)
{
Besant2 obj=new Besant2();
obj.A();
obj.B();
--------------------------------------------------------------------------
final class
-----------------
package Mypack1;
final class Besant1
{
public void A()
{
System.out.println("A method");
}
final public void B()
{
System.out.println("This is B method");
}
}
class Besant2
{
public void A()
{
System.out.println("This is A method");
}
/* public void B()
{
System.out.println("B method");
}*/
}
public class Myclass3
{
public static void main(String[] args)
{
Besant2 obj=new Besant2();
obj.A();
//obj.B();
Besant1 obj1=new Besant1();
obj1.A();
obj1.B();
--------------------------------------------------------------------
Packages
------------------------
---->collection of classes,interface or subpackages..
types:
------------
1)Predefined packages
2)userdefined packages
1)Predefined packages
-----------------------------------------
--->These packages are defined in java library.
EX:
-----
import java.util.Scanner;
2)user defined package
--------------------------------------
--->These packages are created by user requirements.
types:
--------------
import packagename.*;
import packagename.classname;
II.import packagename.methodname
----------------------------------------------------------------
package Pack2;
import Pack1.One;
import Pack1.Three;
import Pack1.Two;
public class Myclass
{
public static void main(String[] args)
{
One obj1=new One();
obj1.add(155, 800);
Two obj2=new Two();
obj2.area(7);
Three obj3=new Three();
obj3.concat("Besant ","Technologies");
}
}
--------------------------------------------------------------------
package Pack2;
//import Pack1.One;
//import Pack1.Three;
//import Pack1.Two;
import Pack1.*;
public class Myclass
{
public static void main(String[] args)
{
// One obj1=new One();
//obj1.add(155, 800);
Two obj2=new Two();
obj2.area(7);
Three obj3=new Three();
obj3.concat("Besant ","Technologies");
}
}
----------------------------------------------------------------------------
access specifier
------------------------------
public
private
protected
private
-------------
package Pack1;
public class One
{
private static void add(int x,int y)
{
System.out.println("Adding two values "+(x+y));
}
public static void main(String[] args)
{
add(15,32);
}
}
--------------------------------------------
protected
-------------------
package Pack1;
class My
{
protected void add(int x,int y)
{
System.out.println("Adding two values "+(x+y));
}
public class One extends My
{
public static void main(String[] args)
{
My obj=new My();
obj.add(56,780);
}
}
-----------------------------------------------------------------------------------
-----------
Exception handling
collections