Abstraction
abstraction in Java is used to hide certain details and only show the essential features of the object.
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
We are going to achieve abstraction using
Interface
Encapsulation
Encapsulation is the packing of data and functions into a single component. Encapsulation is the ability to package data, related behaviour in an object bundle and control/restrict access to them (both data and function) from other objects. It is all about packaging related stuff together and hide them from external elements.
Encapsulation can be achieved by declaring fields in a class as private, while providing access to these fields via public, typically, getter and setter methods. Or else we can tell that the encapsulation can be achieved by java beans.
class Register
{
private String name,email,password;
private int age; private long phone; private double hight;
private char gender;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public long getPhone() { return phone; }
public void setPhone(long phone) { this.phone = phone; }
public double getHight() { return hight; }
public void setHight(double hight) { this.hight = hight; }
public char getGender() { return gender; }
public void setGender(char gender) { this.gender = gender; }
}
{
void insertDetails(Register r)
{
System.out.println("Name is "+r.getName());
System.out.println("Email is "+r.getEmail());
System.out.println("Password is "+r.getPassword());
System.out.println("Age is "+r.getAge());
System.out.println("Gender is "+r.getGender());
System.out.println("Phone number "+r.getPhone());
System.out.println("Height is "+r.getHight()); }
}
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name");
String name = scan.next();
System.out.println("Enter the Email");
String email = scan.next();
System.out.println("Enter the password");
String password = scan.next();
System.out.println("Enter the age");
int age=scan.nextInt();
System.out.println("Enter phone number");
long l = scan.nextLong();
System.out.println("Enter height");
double height = scan.nextDouble();
System.out.println("Enter gender");
char gender =(char) scan.next().charAt(0);
Register r = new Register();
r.setName(name);
r.setEmail(email);
r.setPassword(password);
r.setHight(hight);
r.setAge(age);
r.setGender(gender);
RegisterDetails rd = new RegisterDetails();
rd.insertDetails(r);// hear the bean object is sent to the insertDetails method in RegisterDetails class
}
}
Output
raghu
Enter the Email
raghugbr@gmail.com
Enter the password
12345
Enter the age
27
Enter phone number
9740642202
Enter height 5.9
Enter gender
M
Out put after taking input
Email is raghugbr@gmail.com
Password is 12345
Age is 27
Gender is M
Phone number 9740642202
Height is 5.9
Polymorphism
Polymorphism is the ability by which, we can create functions or reference variables which behaves differently in different programmatic context. Polymorphism is a Greek word which means many(poly) forms(morphism).
Polymorphism is tightly coupled to inheritance and is one of the most powerful advantages to object-oriented technologies.
Polymorphism is essentially considered into two versions.
1. Compile time polymorphism (static binding or method overloading)
2. Runtime polymorphism (dynamic binding or method overriding)
Static binding And Dynamic binding
Static binding | Dynamic binding |
When type of the object is determined at compiled time(by the compiler), it is known as static binding |
When type of the object is determined at runtime, it is known as dynamic binding |
If there is any private, final or static method in a class, there is static binding |
Other methods than private , final , static |
Static binding uses Type(Class in Java) information for binding |
Dynamic binding uses Object to resolve binding. |
Overloaded methods are bonded using static binding |
Overridden methods are bonded using dynamic binding at runtime |
Compile time polymorphism | Runtime polymorphism |
ALSO SEARCH:
"abstraction encapsulation inheritance polymorphism in java with example"
"what is abstraction and encapsulation give real life example"
"give real world example of inheritance polymorphism abstraction and encapsulation"
"what is polymorphism and abstraction"
"difference between abstraction polymorphism and encapsulation with examples"
"what is polymorphism inheritance encapsulation abstraction"
"abstraction and encapsulation real life example"
"abstraction encapsulation inheritance polymorphism in c with example"
"polymorphism and encapsulation in java"