Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class" that is used to group related methods with empty bodies:
note :::
1. no concreate
2. only abstract
why use :::
1. achieve abstraction
2. multiple inheritance
3. lose coupleing
lose coupleing:::
it is when we change in one interface it will not change in other class
syantx :::
interface interface_name{
method (public abstract) , (public static) , (private)
fields (public staic final)
concreate (only default)
}
CONSTRUCTOR :
when the class is run the constructor also run
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
it is like the pojo classes in andorid studio
To achieve this, you must :::
1. declare class variables/attributes as private
2. provide public get and set methods to access and update the value of a private variable
example :::
public class Another {
private int a;
private int b;
private int c;
public Another(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public Another(){
// default
}
public int getA() {
return a;
}
public int getB() {
return b;
}
public int getC() {
return c;
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public void setC(int c) {
this.c = c;
}
}
poly means ( many )
morphism mean ( Form )
polymorphism ( many forms )
example of polymorphism :::
1. water has many forms ( solid , liquid , gas )
2. shapes has many forms ( circle , rectangle , square )
3. sound has many forms ( loin , male , female )
types :::
1. compile time polymorphism ( static polymorphism ) ( handle by compiler )
2. run time polymorphism ( dyanmic polymorphism ) ( handle by jvm )
achieve by :::
1. compile time polymorphism ( method overloading )
2. run time polymorphism ( method overridding )
cases :::
method overloading :::
"1. if we pass character it will call int due to ( automatic promotion )
2. if we pass character it will call object if it is in the arguments
3. if number of arguments (same datatype like in promotion) is large than no automatic promotion
4. main method can be overload
5. changing the return type cannot overload"
method overridding :::
ok ::
"1. return type (covarent return type) change say ho sakta ha
(we can provide parent in parent return type and child ma return type parent ki child honi chiya)
2. access modifier
(child overridding access modifier is greater than parent overridding method)
3. overridding and exception handling
(if parent class overridding method not throw exception child class only throw check exception)
(if parent class overridding method throw exception child class only throw same as parent,samelevel,chlid exception)
4. to inherted abstract class its methods also to be inherted
5. implement ka sab methods ko run karna paray ga
6. synchronized/strictfp can also run "
donot ::
"1. final methods
2. static method
3. private method"
automatic promotion :::
"byte - short - int
char - int
int - float , double , long
float - double
long - float , double
int - float - boolean"
object :::
"object is the parent class of all the classes
( the compile perfer child class to call it first )"
varargs :::
"used for multiple arguments
( it has least property if no method match than it will run )"
difference method overloading and overridding :::
method overloading :::
1. same name
2. same class
3. different arguments ( no.of arg , type of arg , seq of arg )
method overridding :::
1. same name
2. different class
3. same arguments ( no.of arg , type of arg , seq of arg )
4. is-a relationship ( inheritance )
achieve by programing :::
method overloading :::
class test {
void show (){
sout("1")
}
void show(int a){
sout("2")
}
psvm(String[] args){
test test = new test();
test.show();
}
}
method overridding :::
class test {
void show(){
sout("1")
}
}
class xyz extends test {
void show(){
sout("2")
}
psvm(String[] args){
test test = new test();
test.show() -- it will call its own method
xyz xyz = new xyz();
xyz.show() -- it will call its own method
}
}
invoking the overridding method from subclass :::
by using "super" keywoard we can run the parent overridding method
class test {
void show(){
sout("1")
}
}
class xyz extends test {
void show(){
super.show()
sout("2")
}
psvm(String[] args){
test test = new test();
test.show() -- it will call its own method
xyz xyz = new xyz();
xyz.show() -- it will call its own method
}
}
to inherite all properties of parent class into itself is called inheritance
it is alse kown as "is-a" relationship
types of inheritance supported by java :::
1. single (one class pro to other)
2. multilevel (one class pro to a and a class to b class pro to another)
3. hierarical (one class pro to a class and b class)
abstraction is used for security
def :::
1. it is the deatail hiding and implementation of the services
2. data abstraction deals with exposing the interface to the user and hiding the details of implementation
real world example :::
1. in car we will only show break and donot show internal parts
2. in car we will only be showed straing but dont its internal parts
to achieve abstraction :::
1. abstract class ( 0 - 100% )
2. interface ( 100% )
what is in abstract class :::
1. variable (no body)
2. methods (no body)
3. methods modifier "abstract"
4. class modifier "abstract" (it have no objects)
note ::
1. also can be make some concreate method (that have body)
2. the class which inherte abstract class it will make body of abstract method
3. "it will use the concept of method overridding"
example :::
abstract class don{
int butt ;
abstract void start(int a);
}