Encapsulation
Abstraction
Inheritance
Polymorphism
Encapsulation is the technique of making the attribute in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class. For this reason, encapsulation is also referred to as data hiding. import java.io.*; class EncapTest{ private String name="ritesh"; public String getName(){ return name;
}
} class MyFirstProgram{ public static void main(String args[]){
}
EncapTest encap = new EncapTest();
System.out.print("Name : " + encap.getName);
} encap.name
If I write then error will come
Abstraction in Java is achieved by using interface and abstract class
–
Inheritance is the ability of a class to inherit attributes and behaviors from another class.
–
The new class which inherit the attributes and behaviors from a preexisting class is called a child class or derived class or subclass , while the pre-existing class is known as parent class or base class or superclass .
–
Apart from inheriting the attributes and behaviors of the parent class, the child class can have its own attributes and behaviors.
Types Of Inheritance
◦
Single
◦
Multilevel
◦
Hierarchical
◦
Multiple (In java multiple inheritance is implemented only through interfaces due to the problem of Ambiguity )
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Multiple inheritance is not supported by C#, Java directly because they suffer from the problem of ambiguity. They support Multiple inheritance through Interfaces
Problem Of Ambiguity:
Ambiguity takes place when function with same name appears in more than one base classes .This can happens in multiple inheritance in which a class has more than one base classes. In C++ multiple inheritance is supported and problem of ambiguity is resolved by scope resolution operator(::) class ClassA
{ void Show() { }
} class ClassB
{ void Show() { }
} class MyFirstProgram extends ClassA, ClassB
{
} public static void main(String[] arg)
{
MyFirstProgram obj= new MyFirstProgram;
Obj.show(); //ambiguous
Obj.ClassA::Show();
Obj.ClassB::Show();
}
The word polymorphism means many forms .
Polymorphism is of Two Types
(i) Static or Compile Time or Early Binding ( Overloading )
(ii) Dynamic or Run Time or Late Binding ( Overriding , Hiding )