OOPs
OOPs
=====
---->object Oriented Programming Systems
concepts:
-------------------
1)object
2)class
3)Inheritance
4)Polymorphism
5)DataAbstraction
6)Encapsulation
1)object
--------------
---->it is a real world entity.
---->Object is defined by instance of the class.
EX:
-----
car,bike,laptop,mobilephone,house,..
properties:
------------------
EX:car
------
state --->color,model,price,size,..
behaviour --->drive,start,stop,...
2)class
---------------
--->collection of an object is called as a class.
class classname
{
//members(variables)
methods()
class Employee
{
int id,salary;
String name;
login()
logout()
}
class--->state+behaviour
Inheritance
-----------------------
Inheritance is a mechanism where a new class is derived from an existing class.
polymorphism
----------------------------
--->if single task is process by different ways.
DataAbstraction
------------------------------
--->It hiding internal details and show only functionality.
Encapsulation
-------------------------
---->Encapsulation is a process of wrapping data into single unit.
EX:
-----
package BasicPrograms;
import java.util.Scanner;
class Besant
{
public static void First()//static method
{
System.out.println("This is Besant class");
}
}
public class BesantClass
{
2)non-static method
------------------------------------
A method as a non-static in this class,we are called by,
syntax:
------------------
classname obj=new class_name();
obj.methodname();
EX:
----
package BasicPrograms;
import java.util.Scanner;
class Besant
{
public void First()//non-static method
{
System.out.println("This is Besant class");
}
}
public class BesantClass
{
//Besant.First();
}
}
-----------------------------------------------------------------------------------
-------------
Scanner
nextInt()
nextFloat()
String.join()
-----------------------------------------------------------------------------------
--
Inheritance
---------------------
Inheritance is a mechanism where a new class is derived from an existing class.
syntax:
-------------
class classname1 //parent class,base class,superclass
{
EX:
-----
package BasicPrograms;
import java.util.Scanner;
class Besant
{
public void First()//non-static method
{
System.out.println("This is Besant class");
}
}
class A
{
public void Second()
{
System.out.println("This is A class");
}
}
public class BesantClass
{
A obj1=new A();
obj1.Second();
//Besant.First();
}
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 BasicPrograms;
import java.util.Scanner;
class Animal
{
public void walk()
{
System.out.println("Animal is Walking..");
}
}
}
-----------------------------------------------------------------------------
Multilevel Inheritance
---------------------------------------
A (Grand Parent)
|
B (Parent)
|
C (Child)
|
D (grand child)
EX:
----
package BasicPrograms;
import java.util.Scanner;
class Book
{
public void Book_details(String Bname)
{
System.out.println("Book name is "+Bname);
}
}
p1.Book_details("Java");
p1.Author_details("John");
p1.Price_Details(500);
}
EX:
-----
package BasicPrograms;
import java.util.Scanner;
class Book
{
public void Book_details(String Bname)
{
System.out.println("Book name is "+Bname);
}
}
p1.Book_details(Bn);
p1.Author_details(An);
p1.Price_Details(p);
}
}
----------------------------------------------------------------------
Hierarchical Inheritance
-----------------------------------------
A
|
---------------------------
| | | |
B C D E
Ex:
------
package BasicPrograms;
import java.util.Scanner;
class Shape
{
public void Shape_details()
{
System.out.println("Display the Shape Details");
}
}
}
----------------------------------------------------------------------
Multiple Inheritance
-----------------------------------
A B
| |
----------------
|
C
Hybrid Inheritance
---------------------------------
--->combination of hierarchical Inheritance and multiple Inheritance
A
|
----------------------
| |
B C
| |
--------------------------
|
D
-----------------------------------------------------------------------------------
--------
Polymorphism
---------------------------
Poly --->Many
morphism --->differnt forms
12+3=15
"12"+"3"=123
types
----------
1)compile time polymorphism(or)method overloading
2)Run time Polymorphism(or)method overriding
import java.util.Scanner;
class Compiletime
{
public void test()
{
System.out.println("No Arguments");
}
}
public void test(int x,int y,int z)
{
System.out.println(x+y+z*3);
}
}
Ex:
-------
package BasicPrograms;
import java.util.Scanner;
class Compiletime
{
public void test()
{
System.out.println("No Arguments");
}
public void test(float a1)//one arg should be passed but different data type
{
System.out.println(a1);
}
public void test(int x,int y,int z)
{
System.out.println(x+y+z*3);
}
}
}
-----------------------------------------------------------------------------------
----------