- Oops concept
The oops concepts are :-
- Class and objects
- Polymorphism
- Inheritance
- Abstractions
- Encapsulation
classes and objects -
Class is a categories which is contain same type variable and in other language a Class is Modal for creating object. class does not exist in the world (A class is a group of object).
Class is a categories which is contain same type variable and in other language a Class is Modal for creating object. class does not exist in the world (A class is a group of object).
Object is a real time entity, it exist in the world. basically an object created by class.A object have some properties and behavior.
lets take a real time example for more understand about class and object.we can take a real would object like dog, humans, bike,car etc. we know that these all object have some properties and behavior.
lets we take a dog , dog has properties(state) like color, name,age and it has some behavior like barking,running.
similarly the software objects have some properties and behavior like real wold objects.
Example :-
public class Dog{
//dog properties
int age;
String name;
String color;
//dog behavior
void barkign(){
System.out.prinln("dog barking")
}
void running(){
System.out.prinln("dog running")
}
//main methos
public static void main(String args[]){
//create object of dog class
Dog dog = new Dog();
dog.barking();
dog.running();
}
}
for more information about class and object click here
- Methods and data members
In java we have to type of methods and data members
first static type methods and data members
Data members or fields of a Java class declared static are called class members. There exists xactly only one copy of static fields or class members no matter how many objects of the class are finally created. A static field, also called a class variable comes into existence when the Java class is initialized.
Static methods are always which are recommended to perform one operation like opening the files, obtaining a data base connection etc…
Programmatically static methods definitions must be preceded by static keyword.
Static data members are those whose memory space is created only once, whenever the class is loaded in the main memory
second Instance/non-static data members or methods
Instance methods ordata members are those whose memory space is created each and every time whenever an object is created
- A Java static method cannot access instance variables directly.
- ava static methods can only access static data members or fields.
- Java static methods cannot refer this and super
Polymorphism :-
Polymorphism is a ability to create methods in multiple form.there are two types of polymorphism
method overriding and method overloading
Method overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.
Method overloading is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This concept is known as Method Overloading.
Example of Method overloading:-
class Test
{
void demomethod(int x)
{
System.out.println ("x: " + x);
}
void demomethod (int x, int y)
{
System.out.println ("value of x and y : " +x + "," +y);
}
}
class Test2
{
public static void main (String args [])
{
Test Obj = new Test();
Obj .demo(10);
Obj .demo(10, 20);
}
}
this demomethod method overload two time
Example of Method overriding:-
public class PerentClass
{
public void methodOverride()
{
System.out.println ("I'm the method of PerentClass");
}
}
public class ChildClass extends PerentClass
{
public void methodOverride()
{
System.out.println ("I'm the method of ChildClass");
}
}
public class TestClass
{
public static void main (String args []) {
//create object
PerentClass obj1 = new PerentClass();
PerentClass obj2 = new ChildClass();
//invoke methods
obj1.methodOverride();
obj2.methodOverride();
}
}
Inheritance
inheritance is a properties to acquires the properties of parent class (Base calss) from child class.In Java this properties is achieve by extend keyword. this properties can access on modifier public and protected.
// for example of inheritance you can see example of Method overriding(above).
Abstractions
abstraction is a properties of hide unnecessary data and show essential data. For achieving abstraction we can use abstract class and interface.
Encapsulation
wrapping data into single unit is called encapsulation.The class acts like a container encapsulating the properties
wrapping data into single unit is called encapsulation.The class acts like a container encapsulating the properties