0% found this document useful (0 votes)
7 views4 pages

Abstraction

The document explains the concepts of normal and abstract methods, as well as normal and abstract classes in programming. It details how abstract methods lack implementation and how abstract classes can contain abstract methods or none at all, prohibiting direct instantiation. Additionally, it discusses the rules and examples related to abstract classes, including method overloading, constructors, and the definition of data abstraction.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Abstraction

The document explains the concepts of normal and abstract methods, as well as normal and abstract classes in programming. It details how abstract methods lack implementation and how abstract classes can contain abstract methods or none at all, prohibiting direct instantiation. Additionally, it discusses the rules and examples related to abstract classes, including method overloading, constructors, and the definition of data abstraction.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Abstraction

===========
There are two types of methods
a. Normal methods
b. Abstract methods

Normal methods : Contains method declaration & implementation.


void add()
{ logics
}

abstract methods:
The abstract method contains only method declaration but not implementation.
The abstract method must ends with semicolon.
Represent the method is abstract using abstract modifier.

ex: abstract void add();

Based on above representation of methods the classes are divided into two types,
1) Normal classes.
2) Abstract classes.

Normal classes: contains only normal methods.


class Test
{ void add(){}
void mul(){}
void div(){}
}

abstract class:
case 1: The class contains at least one abstract method is called abstract
class.
abstract class Test
{ abstract void add();
abstract void mul();
abstract void div();
}

case 2: The abstract class contians zero abstract methods.


abstract class Test
{ void add(){logics...}
void mul(){logics...}
void div(){logics...}
}

Note : The abstract class may contains abstract methods or may not contains
abstract methods but for the abstract classes object creation is not allowed.

Code style-1
abstract class Services
{ abstract void add(int num1,int num2);
abstract String login(String username,String password);
}

class ServiceImpl extends Services


{ void add(int num1,int num2)
{ System.out.println(num1 + num2);
}
String login(String username,String password)
{ if (username.equals("ram") && password.equals("anu"))
{ return "Success";
}
else
{ return "fail";
}
}
}

class TestClient
{ public static void main(String[] args)
{ // Services s = new Services(); error: Services is abstract; cannot
be instantiated

Services ss = new ServiceImpl();


ss.add(10,20);
String status = ss.login("ram","anu");
System.out.println("Login Status...."+status);
}
}

Note: The abstract class can hold all child classes objects.

Code style-2
ex 2:
abstract class Operations
{ abstract void add(int a,int b);
abstract void mul(int a,int b);
}

abstract class Dev1 extends Operations


{ void add(int a,int b)
{ System.out.println(a+b);
}
}

class Dev2 extends Dev1


{ void mul(int a,int b)
{ System.out.println(a*b);
}
}

class TestClient
{ public static void main(String[] args)
{ Dev2 d = new Dev2();
d.add(10,20);
d.mul(4,5);
}
}

i. If the abstract class contains abstract methods write the implementation in


child classes.
ii. If the child class is unable to provide implementation of all abstract methods,
then declare the child class with abstract modifier, and complete the
remaining method implementations in next created child classes.
iii. It is possible to declare multiple child classes but at final we have to
complete the implementation of all abstract methods.

ex-3:
Observatios:
case 1:
abstract class Message
{ abstract final void morn(String name);
}
error: illegal combination of modifiers: abstract and final

case 2:
abstract class Message
{ abstract static void morn(String name);
}
error: illegal combination of modifiers: abstract and static

case 3:
abstract class Message
{ abstract private void morn(String name);
}
error: illegal combination of modifiers: abstract and private

Note: abstract methods we have to override that methods in child classes but
final,static, private methods not possible to override so the combination of
modifiers are illegal.

case 4: can we overload the abstract methods : yes


abstract class Test
{ abstract void add(int a,int b);
abstract void add(int a,int b,int c);
abstract void add(int a,int b,int c,int d);
}

case 5: can we override abstract methods : yes

ex: Inside the abstract class posible to declare main method.


abstract class Test
{ public static void main(String[] args)
{ System.out.println("This is abstract class main");
}
}

ex:
abstract class Demo
{ Demo()
{ System.out.println("Abstract class: Demo Constructor");
}
}
class Test extends Demo
{ Test()
{ super();
System.out.println("Normal class constructor ");
}
public static void main(String[] args)
{ new Test();
}
}
Note:
Inside the abstract class it is possible to declare the constructors.
It is not possible to execute the abstract class constructor by creating object of
abstract class. But it is possible to execute the abstract class constructor
indirectly by calling from other child classes.
In above example abstract class constructor executed but object is not created.

ex 5: Inside the abstarct class possible to declare,


a. varaibles
b. normal methods & abstract methods
c. constructor
d. blocks (Instance blocks, static blocks)

abstract class Test


{ Test()
{ System.out.println("abstract class cons");
}
{ System.out.println("abstract ins block");
}
static
{ System.out.println("abstract static block");
}
}
class Test1 extends Test
{ Test1()
{ System.out.println("normal class cons");
}
{ System.out.println("normal ins block");
}
static
{ System.out.println("normal static block");
}
public static void main(String[] args)
{ new Test1();
}
}

Abstraction Definition:
Data abstraction is the process of hiding certain details and showing only
essential information to the user.

Data abstraction is the process of hiding implementation details and showing


service details.

Abstraction can be achieved with either abstract classes or interfaces (which


you will learn more about in the next chapter).

You might also like