Abstraction:
Data Abstraction is the process of hiding certain details and showing only the essential information to the user
Abstraction can be achieved either with abstract class or interface in java
Program
abstract class Student
{
abstract void paperarrear();//abstract method
void colgatn()//non abstract method
{
System.out.println(“He will atn the cls regularly”);
}
}
non abstract method: method with declaration as well as definition
abstract method: method that only consists of a declaration part but not the definition
Abstract class must have atleast one abstract method
We can’t able to create an object for abstract class
Abstract class can have abstract method as well as non abstract methods
example 1:
abstract class Member
{
abstract void welcomemsg();//abstract method
void display()//non abstract method
{
System.out.println(“welcome all…”);
}
}
class Student extends Member
{
void welcomemsg()
{
System.out.println(“hello student..”);
}
}
class Teacher extends Member
{
void welcomemsg()
{
System.out.println(“hello Teacher..”);
}
}
class Abstractdemo
{
public static void main(String args[])
{
Student s1=new Student();
s1.display();
s1.welcomemsg();
Teacher t1=new Teacher();
t1.display();
t1.welcomemsg();
}
}
————————-
abstract class Parent
{
abstract void repay();
void getloan()
{
System.out.println(“Getting loan..”);
}
}
class Son extends Parent
{
void repay()
{
System.out.println(“I will pay on behalf of my father…”);
}
}
class Absdemo
{
public static void main(String args[])
{
Son s=new Son();
s.getloan();
s.repay();
}
}
——————-
Interface:(multiple inheritance is possible by using this interface)
interface Animal//Bc1
{
void sound();
void sleep();
}
interface Bird//Bc2
{
void fly();
}
class Dog implements Animal,Bird//Dc
{
void sound()
{
System.out.println(“the dog barks..”);
}
void sleep()
{
System.out.println(“the dog is sleeping..”);
}
void fly()
{
System.out.println(“the bird is flying..”);
}
}
class Interfacedemo
{
public static void main(String args[])
{
Dog o=new Dog();
o.sound();
o.sleep();
o.fly();
}
}
————————–
interface BOB
{
void bobinterest();
}
interface SBI
{
void SBIinterest();
}
class Person implements BOB,SBI
{
void bobinterest()
{
System.out.println(” The BOB interest amount is”+(10000*0.7));
}
void SBIinterest()
{
System.out.println(” The SBI interest amount is”+(10000*0.8));
}
}
class Interface
{
public static void main(String args[])
{
Person a=new Person();
a.bobinterest();
a.SBIinterest();
}
}