Object Oriented
Programmin features
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
1. Abstraction - The
purpose of abstraction is to hide
information that is not relevant or
rather show
only relevant information.
Example
-
To
create an abstract class, just use the abstract
keyword
before the class keyword, in the class declaration.
Now
you cannot instantiate the Employee class normally.
You
have to use Extend keyword after your new class.
For
example suppose you create abstract class
public abstract class Employee { For using methods of employee in another class you have to extend the employee class. Employee e = new Employee("George W.", "Houston, TX", 43); //this will give you error public class Salary extends Employee { // This is right If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as an abstract. public abstract double computePay(); //how abstract method define (); The class containing it must be declared as abstract. Any class inheriting the current class must either override the abstract method or declare itself as abstract. 2. Encapsulation Encapsulation is also called as “Information Hiding”.To achieve encapsulation in Java −
-
Declare the variables of a class as private.
-
Provide public setter and getter methods to modify and view the variables values.
any
class that wants to access the variables should access them through
their getters and setters.
private String name;
now
in another class we cannot direclty access this private variables.
We
have to use their getter and setter method.
GetName();
setName();
3.
Inheritance
Inheritance
can be defined as the process where one class acquires the properties
(methods and fields) of another.
class Super { ..... ..... } class Sub extends Super { ..... ..... }
4.
Polymorphism
public interface Vegetarian{} public class Animal{} public class Deer extends Animal implements Vegetarian{}
Now,
the Deer class is considered to be polymorphic since this has
multiple inheritance.
*
What is super keyword-
It
is used to differentiate the members of superclass from the
members of subclass, if they have same names.
class Super_class { int num = 20; // display method of superclass public void display() { System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; // display method of sub class public void display() { System.out.println("This is the display method of subclass"); } public void my_method() { // Instantiating subclass Sub_class sub = new Sub_class(); // Invoking the display() method of sub class sub.display(); // Invoking the display() method of superclass super.display(); // printing the value of variable num of subclass System.out.println("value of the variable named num in sub class:"+ sub.num); // printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]) { Sub_class obj = new Sub_class(); obj.my_method(); } }
*
what is method overriding
When
invoking a superclass version of an overridden method the super
keyword is used.
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal b = new Dog(); // Animal reference but Dog object b.move(); // runs the method in Dog class } }
*
what is method overloading
If
a class has multiple methods having same name but different in
parameters, it is known as Method
Overloading.
Method
Overloading by changing no. Of arguments
class
Adder{
static
int add(int a,int
b){return a+b;}
static
int add(int a,int
b,int c){return
a+b+c;}
}
class
TestOverloading1{
public
static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Mehod
Overloading by changing data type :
class
Adder{
static
int add(int a,int
b){return a+b;}
static
double add(double a,double
b){return a+b;}
}
class
TestOverloading2{
public
static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}