0% found this document useful (0 votes)
28 views10 pages

BMC 202 Unit II Abstract Methods and Class

The document explains the concepts of normal and abstract methods in Java, highlighting their definitions and differences. It details the structure of normal and abstract classes, the use of the abstract modifier, and the implications of object creation for abstract classes. Additionally, it discusses abstraction and encapsulation, emphasizing their importance in Java programming.

Uploaded by

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

BMC 202 Unit II Abstract Methods and Class

The document explains the concepts of normal and abstract methods in Java, highlighting their definitions and differences. It details the structure of normal and abstract classes, the use of the abstract modifier, and the implications of object creation for abstract classes. Additionally, it discusses abstraction and encapsulation, emphasizing their importance in Java programming.

Uploaded by

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

Abstraction:-

There are two types of methods in java


a. Normal methods
b. Abstract methods
Normal methods:- (component method/concrete method)
Normal method is a method which contains method declaration as well as methodimplementation.
Example:-
void m1() --->method declaration
{
body; --->method implementation
}
Abstract methods:-
1) The method which is having only method declaration but not method implementations such type of
methods are called abstract Methods.
2) In java every abstract method must end with “;”.
Example : - abstract void m1 (); ----> method declaration
Based on above representation of methods the classes are divided into two types
1) Normal classes.
2) Abstract classes.
Normal classes:-
Normal class is a ordinary java class it contains only normal methods if we are trying to declare at least
one abstract method that class will become abstract class.
Example:-
class Test //normal class
{ void m1(){body;} //normal method
void m2(){body;} //normal method
void m3(){body;}//normal method
};
Abstract class:-
Abstract class is a java class which contains at least one abstract method (wrong definition).
If any abstract method inside the class that class must be abstract.
Example 1:-
class Test //abstract class
{
void m1(){}//normal method
void m2(){}//normal method
void m3();//abstract method
};
Example-2:-
class Test //abstract class
{
abstract void m1();//abstract method
abstract void m2();//abstract method
abstract void m3();//abstract method
};
Abstract modifier:-
1 Abstract modifier is applicable for methods and classes but not for variables.
2 To represent particular class is abstract class and particular method is abstract method to the compiler
use abstract modifier.
3 The abstract class contains declaration of methods it says abstract class partially implement class hence
for partially implemented classes object creation is not possible. If we are trying to create object of
abstract class compiler generate error message
“class is abstract con not be instantiated”

Example -1:-
1 Abstract classes are partially implemented classes hence object creation is
not possible.
2 For the abstract classes object creation not possible, if you are trying to
create object compiler will generate error message.
abstract class Test //abstract class
{
abstract void m1(); //abstract method
abstract void m2(); //abstract method
abstract void m3(); //abstract method
void m4(){System.out.println("m4 method");} //normal method
public static void main(String[] args)
{
Test t = new Test();
t.m4();
}
}
Compilation error:- Test is abstract; cannot be instantiated
Test t = new Test();
Example-2 :-
1 Abstract class contains abstract methods for that
abstract methods provide the implementation in child classes.
2 Provide the implementations is nothing but override
the methods in child classes.
3 The abstract class contains declarations but for that
declarations implementation is present in child classes.

abstract class Test


{
abstract void m1();
abstract void m2();
abstract void m3();
void m4(){System.out.println("m4 method");}
}
class Test1 extends Test
{
void m1()
{System.out.println("m1 method");}
void m2()
{System.out.println("m2 method");}
void m3()
{System.out.println("m3 method");}

public static void main(String[] args)


{
Test1 t = new Test1();
t.m1(); t.m2(); t.m3(); t.m4();
Test t1 = new Test1(); //abstract class reference variable Child class object
t1.m1(); //compile : Test runtime : Test1
t1.m2(); //compile : Test runtime : Test1
t1.m3() ; //compile : Test runtime : Test1
t1.m4() ; //compile : Test runtime : Test1

}
}
Example -3 :-
1 Abstract class contains abstract methods for that abstract methods provide the
implementation in child classes.
2 if the child class is unable to provide the implementation of all parent class
abstract methods at that situation declare that class with abstract modifier then
take one more child class to complete the implementation of remaining abstract
methods.
3 It is possible to declare multiple child classes but at final complete the
implementation of all methods.
abstract class Test
{ abstract void m1();
abstract void m2();
abstract void m3();
void m4(){System.out.println("m4 method");}
};
abstract class Test1 extends Test
{ void m1(){System.out.println("m1 method");}
};
abstract class Test2 extends Test1
{ void m2(){System.out.println("m2 method");}
};
class Test3 extends Test2
{ void m3(){System.out.println("m3 method");}
public static void main(String[] args)
{ Test3 t = new Test3();
t.m1();
t.m2();
t.m3();
t.m4();
}
};
Example :- inside the abstract class it is possible to declare
abstract class Test
{ public int a=10;
public final int b=20;
public static final int c=30;
void disp1()
{ System.out.println("a value is="+a);
}
}
class Test1 extends Test
{ void disp2()
{ System.out.println("b value is="+b);
System.out.println("c value is="+c);

}
public static void main(String[] args)
{ Test1 t = new Test1();
t.disp1();
t.disp2();
}
}
Example-5:-
for the abstract methods it is possible to provide any
return type(void, int, char,Boolean…..etc)
class Emp{};
abstract class Test1
{ abstract int m1(char ch);
abstract boolean m2(int a);
abstract Emp m3();
}
abstract class Test2 extends Test1
{ int m1(char ch)
{ System.out.println("char value is:-"+ch);
return 100;
}
}
class Test3 extends Test2
{ boolean m2(int a)
{ System.out.println("int value is:-"+a);
return true;
}
Emp m3()
{ System.out.println("m3 method");
return new Emp();
}
public static void main(String[] args)
{ Test3 t=new Test3();
int a=t.m1('a');
System.out.println("m1() return value is:-"+a);
boolean b=t.m2(111);
System.out.println("m2() return value is:-"+b);
Emp e = t.m3();
System.out.println("m3() return value is:-"+e);
}
};
Example-6:-
It is possible to override non-abstract as a abstract method in child class.
abstract class Test
{ abstract void m1(); //m1() abstract method
void m2(){System.out.println("m2 method");} //m2() normal method
};
abstract class Test1 extends Test
{ void m1(){System.out.println("m1 method");} //m1() normal method
abstract void m2(); //m2() abstract method
};
class FinalClass extends Test1
{ void m2(){System.out.println("FinalClass m2() method");}
public static void main(String[] args)
{ FinalClass f = new FinalClass();
f.m1();
f.m2();
}
};
Example:-
abstract class Test
{ public static void main(String[] args)
{ System.out.println("this is abstract class main");
}
};
Example-8:-
1 Constructor is used to create object (wrong definition).
2 Constructor is executed during object creation to initialize values to instance
variables.
3 Constructors are used to write the functionality that functionality
executed during object creation.
3 There are multiple ways to create object in java but if we are creating
object by using “new” then only constructor executed.
Note :- in below example abstract class constructor is executed but
object is not created.

abstract class Test


{ Test()
{ System.out.println("abstrac calss con");
}
}
class Test1 extends Test
{ Test1()
{ super();
System.out.println("normal class con");
}
public static void main(String[] args)
{ new Test1();
}
};
D:\>java Test1
abstrac calss con
normal class con

case 1:- [abstract method to normal method]


abstract class Test
{ abstract void m1();
};
class Test1 extends Test
{ void m1(){System.out.println("m1 method");}
};
case 2:-[normal method to abstract method]
class Test
{ void m1(){System.out.println("m1 method");}
};
abstract class Test1 extends Test
{ abstract void m1();
};
Example-9:-the abstract class allows zero number of abstract method.
Definition of abstract class:-
Abstract class may contains abstract methods or may not contains abstract methods
but object creation is not possible. The below example contains zero number of
abstract methods.
Ex:- HttpServlet (doesn’t contains abstract methods still it is abstract object
creation not possible )

abstract class Test


{ void cm() { System.out.println("ratan"); }
void pm() { System.out.println("anushka"); }
public static void main(String[] args)
{ Test t = new Test();
t.cm(); t.pm();
}
}
Test.java:6: Test is abstract; cannot be instantiated

Abstraction definition :-
1 The process highlighting the set of services and hiding the internal
implementation is called abstraction.
2 Bank ATM Screens Hiding the internal implementation and highlighting set of
services like ,money transfer, mobile registration,…etc).
3 Syllabus copy of institute just highlighting the contents of java but
implementation there in class rooms .
4 We are achieving abstraction concept by using Abstract classes & Interfaces.

Encapsulation:-
The process of binding the data and code as a single unit is called encapsulation.
We are able to provide more encapsulation by taking the private data(variables)
members.
To get and set the values from private members use getters and setters to set the
data and to get the data.
Example:-
class Encapsulation

{ private int sid;


private int sname;

//mutator methods
public void setSid(int x)

You might also like