What is class:
- class is a Blueprint (or) template of an object
- class is a template is used to create an objects
- class name first must be a capsletter
- It is a logical entity
- it can’t be physical
Syntax in class:
class <class name>
{
//
//
//
}
A class in java contains:
- fields
- methods
- block
- nested class and interface
What is object:
- object is memory reference of a class
- object is real time entity
- object is instantiated from class
- An entity that has state and behaviour is known as object
- It can be physical (or) logical entity
Syntax in object:
class Private //class
{
Private pr = new Private(); //object
}
Object has three characters:
- State–>state represents the data(value)
- Behavior–>it represents the behavior(functionality)
- Identity—>An object identity is typically implemented via a unique ID
What is Abstraction:
- A class which is declared with the abstract keyword is known as abstract class.
- Is a data hiding
- Showing only necessary data hiding unwanted data. Showing only functionality to the user.
- Abstract class can have main method.
- Static and non static can have an abstract class.
- we cannot create object in abstract class.
- Abstract class can have an a constructor but don’t have an object.
- if we want to access method from one to another using abstract key.
- An abstract class must be declared with an abstract keyword.
- In case inside the class one abstract method must change the class name in abstract class.
Syntax in abstraction:
Once we want to access (one to another) the abstract class(or) abstract method use the keyword (Extends)
public abstract class SmartPhone
{
int call;
public void smartphone()
{
System.out.println("smartphone ubder development");
}
public abstract int call(int seconds);
public abstract void sendmessage();
public abstract void receivecall();
public void browser()
{
System.out.println("SmartPhone");
}
}
public class Samsung extends SmartPhone
{
static int price = 5000;
public static void main(String[] args)
{
Samsung sam = new Samsung();
sam.browser();
sam.sendmessage();
sam.receivecall();
//sam.providepattern();
}
public void sendmessage()
{
System.out.println("messages");
}
public void receivecall()
{
System.out.println("call");
}
}
Encapsulation:
- It is data protecting and data Hiding(security).
- this idea of bundling data and methods that work on data within one unit.
- the major advantage of encapsulation in java is data hiding.
- we can allow the programmer to decide on the access of data and methods.
Advantages:
(Flexible programs)–(Easy debugging and testing)–(Reusability)–(Hiding data)
Keywords in encapsulation:
Default–> default key access with in the package.
protected–>use protected key access with in the package.
public–>use public key access in any package.
private–>use private key with in class.
Example: Main reason is a data protecting and data hiding
class Bank
{
int interest =5;
private int salary = 5000;
public static void main(String[] args)
{
Bank rep = new Bank();
rep.deposit();
rep.withdraw();
rep.promote();
System.out.println(rep.interest);
System.out.println(rep.salary);
}
private void promote()
{
System.out.println("Interest Activity");
}
public void deposit()
{
System.out.println("deposit");
}
public void withdraw()
{
System.out.println("withdraw");
}
}