Oops
Oops
OOPS is a technique to write programs that reflect real world scenarios and
represent real world situations like they exist in real world..
That is we write our programs in terms of objects and classes instead of variables
and datatypes.
2) Once the program size increases to 2 or 3 lakh lines of code, then it becomes
unmanageable.
**********************************************************
OOPS
1) Abstraction
2) Encapsulation
3) Class and object
4) Inheritance
5) Polymorphism.
Any language that follows all the above principles can be called as an OOP
language.
Java is 100% pure oop language.
**********************************************************
It is we the programmers define how our classes should be and how they should
behave..
A class is an abstraction that combines (encapsulates) the data and the methods
that can be applied on that data.
ex : class Car{
int year; //attributes....
String model;
//methods....
void startCar(){
syso("Car is started");
}
void run(){
syso("Car is running");
}
void stopCar(){
syso("Car stopped");
}
int x=10;
maruti.year=1999;
maruti.model="zen";
maruti.startCar();
maruti.run();
maruti.stopCar();
}
}
An object is also real world object that can be distinguishable from other such
objects...
The (.) operator is used to reference the class variables and the class methods on
the object.
*********************************************************
method overloading
It is a technique where multiple methods can have the same name but different
signatures..
class A{
int add(int a,int b){
return (a+b);
}
A a=new A();
syso(a.add(10,12));
syso(a.add(12.5,23.5));
syso(a.add(12,23,34));
}
}
Since the compiler can distinguish between the methods by looking at the method
signatures, this is an example of
"compile time polymorphism"...
**********************************************************
constructor
Rules : 1)The name of the constructor should be the same as the class.
2) Constructor cannot return any value.
3) It can be overloaded.
1) Default constructor.
2) Parameterized constructor.
3) Overloaded constructor.
class Student{
int rollno;
String name;
String city;
rollno=r;
name=n;
}
**********************************************************
this keyword
**********************************************************
static keyword
We donot need any object creation to call this variable inside the main.
2) static method : A method, when declared as static, belongs to the entire class
and we can call this method directly inside the main without any object creation...
A static method can access the static variable and also change it.
3) static block : This was introduced from the jdk1.7 onwards.... This is the first
block that gets executed even before the main method....
**********************************************************
Inheritance (18-04-2017)
Inheritance is a technique thru which a class can pass on its attributes and
methods to another class.
A parent can have many children but a child can have only one parent in java.
class A{
int a;
String b;
void start(){}
void run(){}
}
class B extends A{
}
class C extends A{
public static void main(String []args){
B b=new B();
b.start();
b.run();
}
}
**********************************************************
method overriding
It is a technique thru which the child class can change the behaviour it inherited
from the parent class.
********************************************************** class A{
int a;
String b;
void start(){}
void run(){}
}
class B extends A{
void start(){
syso("B started");
}
void run(){
syso("B is running");
}
class C extends A{
void start(){
syso("A started");
}
void run(){
syso("A is running");
}
**********************************************************
note : Since the method to be dispatched depends on the object that we create at
run-time,this overriding can also be called as "run-time polymorphism".
**********************************************************
super keyword
The super keyword is used to reference the super class variable inside the child
class..If both the child class and the super class have the same variable name
inside them, then we can call the super class variable inside the child class using
the keyword "super".
class A{
int speed=100;
void run(){
syso("car is running at speed :" + speed);
}
class B extends A
int speed =120;
void run(){
syso("Car is running at speed :"+ super.speed);
}
**********************************************************
final keyword
It can be used on
**********************************************************
abstract class
An abstract class is a class with atleat one abstract method. An abstract method is
an incomplete method which is declared but not defined in the parent class.
The class that inherits the abstract class should implement the abstract method..
An abstract method starts with the keyword abstract and ends with a semicolon(;).
**********************************************************
abstract class A{
abstract void run();
}
class B extends A{
void run(){
syso("Car is running");
}
public static void main(String args[]){
B b =new B();
b.run();
A a=new A(); //error...
A a=new B(); //reference object....
}
}
Note : We can also create an object of the abstract class but we should point it to
the child class(reference object).
**********************************************************
Interface
First big advantage of interface is Java can implement multiple inheritance thru
the concept of interfaces...
Third advantage : A class can at the same time extend another class and also
implement any number of interfaces...
**********************************************************