You are on page 1of 23

Abstraction

What is Abstraction?
• Making things more general, simpler, abstract

•Exposing only the required essential characteristics and

behavior(Interface)

•Implemented automatically while writing the code in the form of class

and object.
Access Specifiers
•Provide Control access to class members

•Help to implement:
•Encapsulation

•Abstraction

•The private access specifier is generally used to implement encapsulation

•The public access specifier is generally used to implement Abstraction


Class Declaration for point
class Point{

private int x;

private int y;

public void setX(int x)

{ x = (x > 79 ? 79 : ( x < 0 ? 0 : x ) );}

public void setY(int y)

{ y = (y > 79 ? 79 : ( y < 0 ? 0 : y) );}

public int getX( ){ return x;}

public int getY( ){ return y;}}


Class Declaration for point(contd.)
class PointDemo{

public static void main(String args[]){

int a,b;

Point p1=new Point( );


Expected Output:
p1.setX(22); The Value of a is 22
p1.setY(44);
The Value of b is 44
Actual Output:
a=p1.getX( );
The Value of a is 0
System.out.println("The Value of a is " + a); The Value of b is 0
?
b=p1.getY( );

System.out.println("The Value of b is " + b);

}
Class Declaration for point-modified
class Point{

private int x;

private int y;a

public void setX(int x)

{ this.x = (x > 79 ? 79 : ( x < 0 ? 0 : x ) );}

public void setY(int y)

{ this.y = (y > 79 ? 79 : ( y < 0 ? 0 : y) );}

public int getX( ){ return x;}

public int getY( ){ return y;}

}
class PointDemo{

public static void main(String args[]){

int a,b;

Point p1=new Point( );

p1.setX(22);

p1.setY(44);
Output:
a=p1.getX( );
The Value of a is 22
System.out.println("The Value of a is " + a); The Value of b is 44
b=p1.getY( );

System.out.println("The Value of b is " + b);

}
Predict the answer
1. Which among the following best defines abstraction?
a)Hiding the implementation
b) Showing the important data
c) Hiding the important data
d) Hiding the implementation and showing only the features
Predict the answer
2. Abstraction gives higher degree of ________
a) Class usage
b) Program complexity
c) Idealized interface
d) Unstable interface
Predict the output
class Test
{ int a; int b;
Test(int a, int b) Output:
{ a = a; b = b; } this.a=a;this.b=b 00
void display()
{ System.out.println("a = " + a + " b = " + b); }
public static void main(String[] args)
{ Test object = new Test(10, 20); object.display(); }
}
Inheritance
IS-A relationship
HAS-A relationship(Aggregation)

a parent-child relationship between classes

allows sharing of the behavior of the parent class into its


child classes
• Code Reusability
child class can add new behavior or override existing
behavior from parent
Terminology

Base class Super class Parent class

Derived class Sub class Child class


Types-Single
class Vehicle {

class Vehicle String vehicleType; }

extends IS - A
class bike extends Vehicle {

class bike String modelType;


public void showDetail() {
vehicleType = "bike“; modelType = "sports";
Output: System.out.println(modelType+" "+vehicleType); }
sports bike class single
{ public static void main(String[] args)
{ bike car =new bike(); car.showDetail(); } }
Types- Multilevel

class Vehicle
extends
class RegVechicle
extends
class Bike
extends
class MountainBike
Types- Multilevel
class Vehicle{
int noOfWhe;}
class RegisteredVehicle extendsVehicle{
String regNo;}
class Bike extends RegisteredVehicle {
boolean hasHel;}
class MountainBike extends Bike{
double maxEle;}
Types- Multilevel
class TestVehiclesHierarchy
{ public static void main(String arg[]) {
MountainBike mb = new MountainBike();
mb.noOfWhe = 2;
mb.regNo = "APXX WWW";
mb.hasHel= true;
mb.maxEle= 3000.0;
System.out.print("Mountain Bike with registration Number " + mb.regNo);
System.out.println(" is supported till the elevation of " + mb.maxEle + "
feet.");
}
}
Output:
Mountain Bike with registration Number APXX WWW is
supported till the elevation of 3000.0 feet.
Types - Hierarchical

class Species

class Animal Class Bird


Types -Hierarchical
class Species{
void breath(){System.out.println("Species can take breath"); }
}
class Animal extends Species{
void walk(){System.out.println("Animals can walk"); }
}
class Bird extends Species{
void fly(){System.out.println("Birds can fly");}
}
class Demo
{ public static void main(Sring args[]) {
Animal a=new Animal(); Bird b=new Bird();
a.walk(); b.breath(); b.fly();
}
}
Multiple inheritance is not supported in java
(Diamond problem, Achieved through Interfaces)
class A
{ void fun() { System.out.println("Parent1"); }}
class B
{ void fun() { System.out.println("Parent2"); } }

// Error : Test is inheriting from multiple classes

class Test extends A, B


{ Compiler Error
public static void main(String args[])
{ Test t = new Test(); t.fun(); } }
Polymorphism

• Polymorphism means many (poly) shapes (morph)


• Having multiple methods with the same name in
the same class
• There are two kinds of polymorphism:
• Overloading
• Two or more methods with different signatures
• Overriding
• Replacing an inherited method with another having the same signature
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) {
System.out.println("double d = " + d);
}
}
Overriding

class Bank{ class Test2{


int getRateOfInterest(){return 0;} } public static void main(String args[]){
SBI s=new SBI();
class SBI extends Bank{ ICICI i=new ICICI();
int getRateOfInterest(){return 8;} } AXIS a=new AXIS();
System.out.println("SBI Rate of Interest:
class ICICI extends Bank{
"+s.getRateOfInterest());
int getRateOfInterest(){return 7;} } System.out.println("ICICI Rate of Interest:
class AXIS extends Bank{ "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest:
int getRateOfInterest(){return 9;} } "+a.getRateOfInterest()); } }
THANK YOU

You might also like