Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Abstraction in Java

Abstraction in Java involves hiding implementation details and exposing only essential features, achieved through abstract classes and interfaces. Abstract classes can contain abstract methods that must be implemented in subclasses, while interfaces define methods without implementations. The document also discusses the differences between classes and interfaces, including instantiation, inheritance, and method definitions.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Abstraction in Java

Abstraction in Java involves hiding implementation details and exposing only essential features, achieved through abstract classes and interfaces. Abstract classes can contain abstract methods that must be implemented in subclasses, while interfaces define methods without implementations. The document also discusses the differences between classes and interfaces, including instantiation, inheritance, and method definitions.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Abstraction in Java

Abstraction in Java is the process of hiding the implementation details and only showing the essential
functionality or features to the user. This helps simplify the system by focusing on what an object does
rather than how it does it. The unnecessary details or complexities are not displayed to the user.

In Java programming, abstraction is achieved using Abstract classes and interfaces

Abstract Classes and Abstract Methods

• An abstract class is a class that is declared with an abstract keyword.


• An abstract method is a method that is declared without implementation.
• An abstract class may or may not have all abstract methods. Some of them can be concrete
methods
• A abstract method must always be redefined in the subclass, thus making overriding compulsory
or making the subclass itself abstract.
• Any class that contains one or more abstract methods must also be declared with an abstract
keyword.
• There can be no object of an abstract class. That is, an abstract class can not be directly
instantiated with the new operator.
• An abstract class can have parameterized constructors and the default constructor is always
present in an abstract class.

// Java program to illustrate the concept of Abstraction

abstract class Shape


{
String color;

// these are abstract methods


abstract double area();
public abstract String toString();

// abstract class can have the constructor


public Shape(String color)
{
System.out.println("Shape constructor called");
this.color = color;
}

// this is a concrete method


public String getColor()
{
return color;
}
}
class Circle extends Shape
{
double radius;
public Circle(String color, double radius)
1
{

// calling Shape constructor


super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}

@Override double area()


{
return Math.PI * Math.pow(radius, 2);
}

@Override public String toString()


{
return "Circle color is " + super.getColor() + "and area is : " + area();
}
}
class Rectangle extends Shape {

double length;
double width;

public Rectangle(String color, double length, double width)


{
// calling Shape constructor
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}

@Override double area()


{
return length * width;
}
@Override public String toString()
{
return "Rectangle color is " + super.getColor() + "and area is : " + area();
}
}
public class Test
{
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}

2
}
// Another example of Abstract Class declared

abstract class Animal


{
private String name;
public Animal(String name)
{
this.name = name;
}

public abstract void makeSound();

public String getName()


{
return name;
}
}

// Abstracted class
class Dog extends Animal
{
public Dog(String name)
{
super(name);
}

public void makeSound()


{
System.out.println(getName() + " barks");
}
}

// Abstracted class
class Cat extends Animal
{
public Cat(String name)
{
super(name);
}

public void makeSound()


{
System.out.println(getName() + " meows");
}
}

// Driver Class
public class Geeks
{

3
public static void main(String[] args)
{
Animal myDog = new Dog("ABC");
Animal myCat = new Cat("XYZ");

myDog.makeSound();
myCat.makeSound();
}
}

Interface
Interfaces are another method of implementing abstraction in Java. It is a collection of abstract methods.
The interface is used to achieve abstraction in which you can define methods without their
implementations (without having the body of the methods). An interface is similar to the class.

Declaring an Interface in Java


The interface keyword is used to declare an interface.

public interface NameOfInterface


{
// Any number of final, static fields
// Any number of abstract method declarations\
}

Implementing Interfaces in Java


A class uses the implements keyword to implement an interface. The implements keyword appears in
the class declaration following the extends portion of the declaration.

Example:
interface Animal
{
public void eat();
public void travel();
}
public class MammalInt implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}

public void travel()


{
System.out.println("Mammal travels");
}
public int noOfLegs()
{
return 0;
}
public static void main(String args[])

4
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

Output
Mammal eats
Mammal travels

Features Class Interface

The keyword used to create a The keyword used to create an


Keyword class is “class”. interface is “interface”.

A class can be instantiated


The keyword used to create an
i.e., objects of a class can be
interface is “interface”
Instantiation created.

Classes do not support Interface supports multiple


Inheritance multiple inheritance. inheritance.

A class can implement an


A class can inherit another interface using the keyword
class using the keyword “implements”. An interface can
Inheritance extends. inherit another interface using
Mechanism “extends”.

Constructors It can contain constructors. It cannot contain constructors.

An interface contains abstract


Methods in a class can be methods by default (before Java
abstract, concrete, or both. 8) or default/static methods
Methods (from Java 8 onward).

Variables and methods in a


class can be declared using All variables and methods in an
Access any access specifier(public, interface are declared as public
Specifiers private, default, protected).

Variables in a class can be


All variables are static and final.
Variables static, final, or neither.

A class is a blueprint for An interface specifies a contract


creating objects and for classes to implement by
encapsulates data and focusing on capabilities rather
Purpose behavior. than implementation

5
Extending Java Interfaces
An interface can extend another interface in the same way that a class can extend another class. The
extends keyword is used to extend an interface, and the child interface inherits the methods of the
parent interface.

interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}

interface Football extends Sports


{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}

interface Hockey extends Sports


{
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}

public class HockeyDemo implements Hockey


{

public void setHomeTeam(String name)


{
System.out.println("Home team: " + name);
}

public void setVisitingTeam(String name)


{
}
public void homeGoalScored() {}

public void visitingGoalScored() {}

public void endOfPeriod(int period) {}

6
public void overtimePeriod(int ot) {}

public static void main(String[] args)


{
Hockey hockeyDemo = new HockeyDemo();
hockeyDemo.setHomeTeam("India");
}
}

Output
Home team: India

The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements
Hockey needs to implement all six methods. Similarly, a class that implements Football needs to define
the three methods from Football and the two methods from Sports.

An interface can also extend multiple interfaces.

// multiple inheritance in interfaces

import java.io.*;
interface intfA
{
void geekName();
}

interface intfB
{
void geekInstitute();
}

interface intfC extends intfA, intfB


{
void geekBranch();
}

// class implements both interfaces and provides implementation to the method.


class sample implements intfC
{
public void geekName()
{
System.out.println("Rohit");
}

public void geekInstitute()


{
System.out.println("JIIT");
}

public void geekBranch()

7
{
System.out.println("CSE");
}

public static void main(String[] args)


{
sample ob1 = new sample();

// calling the method implemented


// within the class.
ob1.geekName();
ob1.geekInstitute();
ob1.geekBranch();
}
}

Output
Rohit
JIIT
CSE

You might also like